Short for concentrate, the
cat command is one of the most commonly-used commands in Linux. It is used to view the content of a file, create a file or multiple files, concentrate files as well as redirect output in Terminal or files.
The general syntax of the
cat command is as follow:
$ cat [OPTION] [FILE]...
In this article, I am going to show you various ways to use the
cat commands that will add value and productivity to your work.
Let’s take a look.
cat to create a new file and add content
cat > filename
Example: Create a new file call vegetables.txt.
cat > vegetables.txt
After hitting Enter, a new file named vegetables.txt will be created, and the terminal will now await user’s input for the file’s content.
Go on and input the content; to save and exit, hit the Control + D shortcut key.
Display file’s content with
Example: Display the content of vegetables.txt.
Display content of multiple files with
cat filename_1 filename_2
Example: View both the content of fruits.txt and vegetables.txt.
cat fruits.txt vegetables.txt
Display content with line numbering with
cat -n filename
Example: View the content of fruits.txt, accompanied by line numbers.
cat -n fruits.txt
Copy, replace, or replicate a file’s content using
cat filename new_filename
Example: Duplicate fruits.txt into a new file named new_fruits.txt. If new_fruits.txt already exists, its existing content will be replaced with fruits.txt‘s content.
cat fruits.txt > new_fruits.txt
Note: The command
cat foo > bar suggests that foo’s content will be copied over to bar. If you want to copy bar’s content to foo instead, use this command
cat foo < bar.
Merge multiple files’ content into one with
cat filename_1 filename_2 > filename_3
Example: Merge/combine content of both fruits.txt and vegetable.txt into a new file named grocery.txt.
cat fruits.txt vegetable.txt > grocery.txt