The cat command stands for “concatenate” which is a commonly used command in Linux & macOS. This command has multiple uses like creating a file, reading a file, concatenating two or more files, and printing its output directly on the terminal. In other words, it can be rightly assumed that the cat command is predominantly used to display the content of any file on the terminal as well as to concatenate standard input (stdin) to standard output (stdout) streams. You can use CAT command piped with several other commands such as GREP or even as Docker sub-commands.
Syntax of the CAT command:
cat [OPTION].... [FILE]...
Options:
A: It is used to display the entire content of the file.
b: It is used to print each line along with its line number (excluding empty lines).
E: It is used to display the end of each line by appending ‘&’ at the end.
n: It is used to print each line along with its line number (including empty lines).
s: It is used to remove multiple empty lines.
T: It is used to replace TAB characters with ^I.
u: It is used to ignore things.
v: It shows non-printable characters and uses ^ and M- notation, except for TAB and LFD.
help: It is used to get all information about cat commands like syntax, uses of each option, etc.
version: It is used to check the current version of the cat command.
Example 1: Creating a new file.
To create a new file, we can use the cat command inside the terminal as mentioned below. Let us create a file “example.txt” and write some content in it.
$ cat > FILE_NAME.txt
On executing the above command, it will create a new file and prompt us to write the content. After writing the desired content in the file, we can press Ctrl/Cmd + D to exit from the file and save the content.
Example 2: Displaying the content of the file on the terminal.
To display the content of a file inside the terminal, we can use the cat command along with the file name of the desired file. This will print all the contents of the file in the terminal.
$ cat FILE_NAME.txt
If the size of the file is quite large (>5MB), then it may take some time to display all the contents.
Example 3: Copying content of a file to another file.
To copy the content of one file to another file, we can use the > (Shell direction) character along with the cat command. In the example given below, suppose a file file1.txt has a single line with “This text is being copied to file2.txt file” written in it and we are copying this to file2.txt file.
$ cat file1.txt > file2.txt
If file2.txt already exists, then the content will be written in the existing file only.
Example 4: Appending the content of a file to another file.
To append the content of a file to another file, we will use double-shell direction (>>) along with the cat command.
Suppose we have two files called Planets.txt and Stars.txt.
Planets.txt contains two lines:
Earth is a planet.
Mars is a planet.
Stars.txt contains two lines:
Polaris is a star.
Alpha Centauri.
The command to append the content of these two files is given below.
$ cat Planets.txt >> Stars.txt
Now, the content of Planet.txt is appended at the end of Stars.txt.
Example 5: Merging content of all files into a single file.
To merge the content of all files into a single file, we will again use shell direction along with cat command. But this time, we will merge all the files with the .txt extension into merged.txt file. We know that in Linux, * is a special character which means “all”.
$ cat *.txt > merged.txt
This will merge the content of all files with the extension .txt to merged.txt To display the content of merged.txt, we can use the cat command again.
Example 6: Adding line number to the output.
To display line numbers before each line of output, we can use the -n option of the cat command. This will add line numbers at the start of each line.
$ cat -n merged.txt
Example 7: To print the end of the line.
To find the end of each line in a file, we can use the cat command with the -E option. It will append ‘&’ at the end of each line. Basically, it is used to check if there are any trailing spaces at the end of the file.
$ cat -E merged.txt
Example 8: Displaying Tabs.
With the help of the cat command, we can replace TAB with ^I. To achieve this, we have to use the -T option along with the cat command.
$ cat -T showTabs.txt
Suppose we have a file called “showTabs.txt” with the following content in it.
“ 1 tab is in starting and 2 at last
2 tabs in starting and 1 at last “
Now, if we use the -T option along with the cat command, then we will get the following output.
Example 9: To check the version.
To find the version of the cat command, we can use the following command.
$ cat --version
Wrapping Up
In this article, we have discussed the cat command in Linux. We discussed the syntax of the cat command and multiple examples and use-case scenarios along with their respective outputs.
We sincerely hope that this article gave you a quick glimpse of the CAT command in Linux. Do let us know in the comments if you have any queries or suggestions. We will have our experts solve them for you.
Happy Learning!
Top comments (0)