Using Cat Command in Linux with Examples

In Linux, the cat command is one of the most commonly used commands. cat which stands for "concatenate", can read, write and concatenate file contents to standard output. The cat command is usually used to view the contents of one or more text files, combine files by adding contents of one file to another and create new files.

In this tutorial, we will learn how to use the cat command to its full potential with examples.

General Syntax

Syntax:

$ cat [options] [filename/filenames]

The [options] allows you to use different arguments to the command.

[filename/filenames] is where you can specify the name of a file or multiple files that you wish to display.

Create a new file

Using the cat command, you can create a new file and add content.

Syntax:

$ cat > [filename]

When you create a file in this manner, the cursor is placed on a new line where you can type your contents. After you have written the desired content, you can use Ctrl+D to finish editing and save it.

For Example:

$ cat > Test1.txt

Output:

kali@kali:~/Desktop$ cat > Test1.txt
This is the first test file. 

Display Contents of a file

You may use the cat command to display the contents of a file by simply typing cat followed by the filename.

Syntax:

$ cat [filename]

You can use more or less command if you want to view file page by page.

Syntax:

$ cat [filename] | more
$ cat [filename] | less

For Example:

$ cat Test1.txt

Output:

kali@kali:~/Desktop$ cat Test1.txt 
This is the first test file.
kali@kali:~/Desktop$ 

Display Contents of Multiple files

The cat command can be used to display the contents of multiple files at a time. You can use the cat command followed by the list of filenames separated with spaces.

Syntax:

$ cat [filename1] [filename2] ....

Example:

$ cat Test1.txt Test2.txt Test3.txt 

Output:

kali@kali:~/Desktop$ cat Test1.txt Test2.txt Test3.txt 
This is the first test file.
This is second test file.
This is the third test file.
kali@kali:~/Desktop$ 

Copy Contents from one file to another

Using the (>) operator in the cat command, we can copy the contents from one file to another.

Syntax:

$ cat [filename1] > [filename2]

If the [filename2] does not exist, then the cat command automatically creates a new one and copies the file of [filename1] to [filename2].

For Example:

$ cat Test1.txt > Test2.txt

Output:

kali@kali:~/Desktop$ cat Test1.txt > Test2.txt 
kali@kali:~/Desktop$ cat Test2.txt 
This is the first test file.
kali@kali:~/Desktop$ 

The command will copy the content from Test1.txt and overwrites it in Test2.txt. Instead of overwriting it, you can also append a source text file to the destination text file using the (>>) operator.

Syntax:

$ cat [filename1] >> [filename2]

For Example:

$ cat Test1.txt >> Test2.txt

Output:

kali@kali:~/Desktop$ cat Test2.txt 
This is the second test file.
kali@kali:~/Desktop$ cat Test1.txt 
This is the first test file.
kali@kali:~/Desktop$ cat Test1.txt >> Test2.txt 
kali@kali:~/Desktop$ cat Test2.txt 
This is the second test file.
This is the first test file.
kali@kali:~/Desktop$ 

Display Line Number in a file

All the not-empty files can be displayed using the -b flag along with the cat command and file name.

Syntax:

$ cat -b [filename]

For example:

$ cat -b Test1.txt

Output:

kali@kali:~/Desktop$ cat -b Test1.txt 
  1  This is the first test file.
  2  This   3  is   4  test  5  file.
kali@kali:~/Desktop$ 

If you want to display the lines with no characters as well then, the -n flag can be used along with the cat command and filename.

Syntax:

$ cat -n [filename]

For Example:

$ cat -n Test1.txt

Output:

kali@kali:~/Desktop$ cat -n Test1.txt 
      1  This is the first test file.
      2
      3  This 
      4  is 
      5  test
      6
      7
      8  file.
      9
 kali@kali:~/Desktop$ 

Concatenate a file or multiple files

If you want to view the contents of multiple files at once, use the cat command to concatenate them.

Syntax:

$ cat [filename1] [filename2]...

For Example:

$ cat Test1.txt Test2.txt Test3.txt

Output:

kali@kali:~/Desktop$ cat Test1.txt Test2.txt Test3.txt 
This is the first test file.
This is the second test file.
This is the first test file.
This is the third test file.
kali@kali:~/Desktop$ 

You can also combine multiple files into a single file by creating a new file or updating an existing one using the (>) operator.

Syntax:

$ cat [filename1] [filename2]... > [filename3]

For Example:

$ cat Test1.txt Test2.txt Test3.txt > Test4.txt

Since Test4.txt does not exist, it creates and a new file called Test4.txt and concatenates the contents of Test1.txt, Test2.txt, and Test3.txt in Test4.txt.

Output:

kali@kali:~/Desktop$ cat Test1.txt Test2.txt Test3.txt > Test4.txt
kali@kali:~/Desktop$ cat Test4.txt 
This is the first test file.
This is the second test file.
This is the first test file.
This is the third test file.
kali@kali:~/Desktop$ 

Display End of each line in a file

You can determine the end of a line in a file using the cat command. Sometimes there are hidden characters such as spaces at the end of a line that can bring error or discover problems. You can use the cat command with the -E flag to show dollar ($) as an end of line character. 

Syntax:

$ cat -E [filename]

For Example:

$ cat -E Test4.txt

Output:

kali@kali:~/Desktop$ cat -E Test4.txt 
This is the first test file.$
$
$
This is the second test file.$
$
$
This is the first test file.$
$
$
This is the third test file.$
kali@kali:~/Desktop$ 

Reduce Blank Lines

When you display the contents in a file, it might be disturbing to see lots of blank lines. The cat command along with -s can be used to remove repeated blank lines from the output. The -s option in the cat command displays only one blank line and compresses the repeated ones.

Syntax:

$ cat -s [filename]

For Example:

$ cat -s Test4.txt

Output:

kali@kali:~/Desktop$ cat Test4.txt 
This is the first test file.





This is the second test file.




This is the first test file.




This is the third test file.

kali@kali:~/Desktop$ cat -s Test4.txt 
This is the first test file.

This is the second test file.

This is the first test file.

This is the third test file.
kali@kali:~/Desktop$  

The first output is without using the -s option and the second output is after using the -s option.

Show Tabs

The -T option along with the cat command displays the file content as well as the tab space within the text.
The tab spaces are denoted by the symbol ^I.

Syntax:

$ cat -T [filename]

For Example:

$ cat -T Test4.txt

Output:

kali@kali:~/Desktop$ cat Test4.txt 
         This is the first test file.
This is the second test file.
         This is the first test file.
This is the     third test file.

kali@kali:~/Desktop$ cat -T Test4.txt 
^IThis is the first test file.
This is the second test file.
^IThis is the first test file.
This is the ^Ithird test file.
kali@kali:~/Desktop$ 

The first output is without using the -T option and the second output is after using the -T option.

Display contents of a file in reverse order

The tac command is the inverse of the cat command. tac will display the output in the reverse order of the text file's contents.

Syntax:

$ tac [filename]

For Example:

$ tac Test4.txt

Output:

kali@kali:~/Desktop$ cat Test4.txt 
This is the first test file.
This is the second test file.
This is the first test file.
This is the third test file.

kali@kali:~/Desktop$ tac Test4.txt 
This is the third test file.
This is the first test file.
This is the second test file.
This is the first test file.

The first output is obtained with the cat command and the second output is obtained using the tac command.

Use the help command if you want to learn more about the cat command or if you have any confusion.

$ cat --help

Output:

kali@kali:~$ cat --help
Usage: cat [OPTION]… [FILE]…
Concatenate FILE(s) to standard output.
With no FILE, or when FILE is -, read standard input.
 -A, --show-all           equivalent to -vET
   -b, --number-nonblank    number nonempty output lines, overrides -n
   -e                       equivalent to -vE
   -E, --show-ends          display $ at end of each line
   -n, --number             number all output lines
   -s, --squeeze-blank      suppress repeated empty output lines
   -t                       equivalent to -vT
   -T, --show-tabs          display TAB characters as ^I
   -u                       (ignored)
   -v, --show-nonprinting   use ^ and M- notation, except for LFD and TAB
       --help     display this help and exit
       --version  output version information and exit
Examples:
   cat f - g  Output f's contents, then standard input, then g's contents.
   cat        Copy standard input to standard output.
kali@kali:~$ 

Conclusion

In this tutorial, we learned about the cat command, its usage with various options, and examples. Cat is a useful command that lets you create and view multiple kinds of text files. Multiple files can be displayed at the same time in multiple ways using the cat command.

Leave a Comment