cat Command Cheatsheet
The cat (concatenate) command is used to display the contents of a file on the standard output. It's often used for quick file viewing, creating files, or combining multiple files.
Common Usage
| Command | Description |
|---|---|
cat [FILE] | Displays the contents of a single file to the terminal. |
cat file1.txt file2.txt | Displays the contents of `file1.txt` and `file2.txt` one after the other. |
cat > newfile.txt | Creates a new file. The command will wait for you to type input. Press `Ctrl+D` to save and exit. |
cat file1.txt >> file2.txt | Appends the content of `file1.txt` to the end of `file2.txt`. |
Key `cat` Options
| Option | Description |
|---|---|
-n | Displays the contents of the file with line numbers. |
-b | Displays line numbers for non-blank lines only. |
-s | Squeezes multiple adjacent blank lines into a single blank line. |
-E | Displays a `$` character at the end of each line. |
`cat` vs `less`
While `cat` is great for small files, it is not recommended for large files as it prints the entire content to the terminal at once. This can make the terminal difficult to use. For large files, `less` is the preferred tool as it provides an interactive viewer that only loads a portion of the file at a time.