cat command in Linux

In this article, we cover the cat command in Linux. The command mainly accepts the file data as input and displays it on the standard output. cat is taken from the word “catenate”.

We can also use multiple file arguments with the command. Understand its various command-line options through examples next.

cat command in Linux

Example I. View contents of a file in standard output.

There are two ways to get this done. Either do

cat [filename]


cat < [filename]

both will yield similar results. For instance,

cat test-file.txt
cat < test-file.txt

Example II. View contents of multiple files in standard output.

The following command would display the contents of each file one after the other.

cat [filename1] [filename2]

For instance,

cat test-file1.txt test-file2.txt

Example III. Create a new file with the cat command:

cat > [filename]

Thereafter, we can enter text, and when done Press Ctrl + C. This would save the file in the current directory.

For instance,

cat > test-file.txt

Write “Hello World!” next and Press Ctrl + C. To view the contents of a file:

cat test-file.txt

Example IV. Number blank and non-blank lines in a file:

cat -n [filename]

Example V. Number only non-blank lines in a file:

cat -b [filename]

Example VI. Display $ at the end of each line in a file:

cat -E [filename]

Example VII. Merge multiple files in a new file:

cat [filename1] [filename2] > [merged-file]

Example VIII. Write the contents of a file to an already existing file. The content gets added to the end of an already existing file.

cat [filename] >> [existing-file]

In conclusion, we have covered the cat command-line utility in Linux here.

Similar Posts