Working with Files through the Command Line
Tags: linux, shell, terminal, webdev
Published: 2023-08-07 10:05:47

## `cat`

- The cat command con**cat**enates and prints the contents of files.
- `cat <filename>` will read the contents of a file and print them out. For example, `cat instructions.txt` will read in from the instructions.txt file and then print the contents out to the screen.
- If we provide cat with multiple files, it will concatenate their contents and output them.
- `cat peanutbutter.js jelly.css` will output peanutbutter.js first and immediately after print the contents of jelly.css
    
    ```bash
    cat <filename>
    cat <file1> <file2>
    ```
    

## `less`

- The `less` command displays the contents of a file, one page at a time. We can navigate forwards and backwards through the file, which is especially useful with very large files.
- `less somefile.txt` will display the contents of somefile.txt using less.
- When viewing a file using less :
    - press **space** to go to the next page of the file
    - press **b** to go back to the previous page
    - press **Enter** or **Down arrow** to scroll by one line
    - to search, type forward slash **/** followed by a pattern
    - press **q** to quit
        
        ```bash
        less <filename>
        ```
        

## `tac`

- tac (cat spelled backwards) will concatenate and print files in reverse. It prints each line of a file, starting with the last line. You can think of it as printing in reverse "vertically"
    
    ```bash
    tac file.txt
    ```

![tac](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3buf156hbm6aqkj8tapv.png)
    

## `rev`

- The `rev` command prints the contents of a file, reversing the order of each line. Think of it as a "horizontal" reverse, whereas tac is a "vertical" reverse.
    
    ```bash
    rev file.txt
    ```
    
    
![rev](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/zxrb7ll1tp13dgvo7977.png)
    

## `head`

- The head command prints a portion of a file, starting from the beginning of the file. By default, it prints the first 10 lines of a file.
- `head warAndPeace.txt` would print the first 10 lines of the warAndPeace.txt file
- We can also specify a number of lines for head to print using the **-n** option (or --lines) followed by an integer.
- head -n 21 warAndPeace.txt would print the first 21 lines of the warAndPeace.txt file
- We can also use an even shorter syntax to specify a number of lines: `head -3 filename.txt` will print the first 3 lines of the file.
    
    ```bash
    head filename.txt
    head -n 13 filename.txt
    ```
    

## `tail`

- The tail command works similarly to the head command, except it prints from the END of a file. By default, it prints the last 10 lines of a file.
- `tail warAndPeace.txt` would print the last 10 lines of the warAndPeace.txt file
- The same -n option with head also works with the tail command.
    
    ```bash
    tail filename.txt
    ```
    

## `wc`

- The word count command can tell us the number of words, lines, or bytes in files. By default, it prints out three numbers: the lines, words, and bytes in a file.
- We can use the **-l** option to limit the output to the number of lines.
- The **-w** option limits the output to the number of words in the file.
    
    ```bash
    wc -l students.txt
    ```
    

## `sort`

- The sort command outputs the sorted contents of a file (it does not change the file itself). By default, it will sort the lines of a file alphabetically.
- `sort names.txt` would print each line from names.txt, sorted in alphabetical order
- The **-r** option tells the sort command to sort in reverse order.
- `sort names.txt -r` would print each line from names.txt, sorted in REVERSE alphabetical order.
    
    ```bash
    sort filename.txt
    sort -r filename.txt
    ```
    
    ### Sorting Numerically
    
    - The **-n** option tells the sort command to sort using numerical order.
    - `sort -n prices.txt` would print each line from names.txt, sorted in numerical order.
    - We could also reverse it with `sort -nr prices.txt`
    - The **-u** option tells the sort command to ignore duplicates and instead only sort unique values
        
        ```bash
        sort -n prices.txt
        sort -u prices.txt
        ```