How to Use the Cat Command in Linux
The cat command, short for “concatenate,” is a commonly-used tool in Linux, enabling users to view, create, and concentrate files, or redirect their output. In this post, we’ll explore how both new and experienced Linux users can leverage the cat command for various tasks.
The general syntax of the cat command is as follow:
$ cat [OPTION] [FILE]...
1. Use cat to create a new file and add content
cat > filename
Example:
The command cat > vegetables.txt takes the standard input and redirects it to a file named “vegetables.txt”. When you execute this command, your terminal doesn’t display any output, but instead waits for you to enter text from the keyboard.
Whatever you type will be written to “vegetables.txt”. To finish and save the file, you’ll need to press CTRL-D (or CTRL-Z on Windows systems using some terminal applications).
Here’s an example of how you might use it:
$ cat > vegetables.txt Carrot Broccoli Spinach ^D
At this point, a file named “vegetables.txt” would be created with the following content:
Carrot Broccoli Spinach
2. Display file’s content with cat
cat filename
Example:
Assume the file contains a list of common vegetables; executing the command cat vegetables.txt would display the contents of the file in the terminal.
Carrots Broccoli Spinach Tomatoes Cucumbers Peppers Onions Potatoes Kale Lettuce
3. Display content of multiple files with cat
cat filename_1 filename_2
Example:
Assuming the contents of the files fruits.txt and vegetables.txt are respectively as follows:
Apple Banana Cherry
Carrot Lettuce Tomato
The command cat fruits.txt vegetables.txt would produce:
Apple Banana Cherry Carrot Lettuce Tomato
4. Display content with line numbering with cat
cat -n filename
Example:
If the contents of fruits.txt are the same as in the previous example:
Apple Banana Cherry
Then the command cat -n fruits.txt would produce:
1 Apple
2 Banana
3 Cherry
5. Copy, replace, or replicate a file’s content using cat
cat filename new_filename
Example:
The command cat fruits.txt > new_fruits.txt will take the contents of fruits.txt and write them into a new file named new_fruits.txt. This command won’t display any output to the terminal, assuming it executes successfully.
If fruits.txt has the same content as before:
Apple Banana Cherry
After running the command, the content of new_fruits.txt will be:
Apple Banana Cherry
6. Merge multiple files’ content into one with cat
cat filename_1 filename_2 > filename_3
Example:
The command cat fruits.txt vegetable.txt > grocery.txt will concatenate the contents of fruits.txt and vegetable.txt, then redirect the output to a file called grocery.txt. If either of the input files doesn’t exist, an error message will be displayed in the terminal, but you won’t see the concatenated contents in the terminal because they are being redirected to grocery.txt.
Suppose fruits.txt contains:
Apple Banana Cherry
And vegetable.txt contains:
Carrot Lettuce Tomato
After running the command, the terminal won’t show any output (unless there’s an error), but the file grocery.txt will contain:
Apple Banana Cherry Carrot Lettuce Tomato
If vegetable.txt was misspelled or doesn’t exist, you would see an error in the terminal similar to:
cat: vegetable.txt: No such file or directory
More Linux commands:
| Directory Operations | rmdir · cd · pwd · exa · ls |
| File Operations | cat · cp · dd · less · touch · ln · rename · more · head |
| File System Operations | chown · mkfs · locate |
| Networking | ping · curl · wget · iptables · mtr |
| Search and Text Processing | find · grep · sed · whatis · ripgrep · fd · tldr |
| System Information and Management | env · history · top · who · htop · glances · lsof |
| User and Session Management | screen · su · sudo · open |