The cat command

Yaser Rahmati | یاسر رحمتی

The cat command allows us to create single or multiple files, to view the content of a file or to concatenate files and redirect the output to the terminal or files.

The "cat" stands for 'concatenate.' and it's one of the most frequently used commands in the Linux terminal.

Examples of uses:

  • To display the content of a file in terminal:

[root@academy tmp]# cat myfile.txt
hello world from Yaser Rahmati

[root@academy tmp]#
  • To display the content of multiple files in terminal:

[root@academy tmp]# cat file1.txt file2.txt
content inside file1.txt
content inside file2.txt
[root@academy tmp]#
  • To display all files in current directory with the same filetype:

[root@academy tmp]# ls
apa.txt  file1.txt  file3.txt         myfile.txt  myfolder2  yum.log
F1       file2.txt  ks-script-ggLlAt  myfolder    myfolder3
[root@academy tmp]# cat *.txt
content inside file1.txt
content inside file2.txt
hello world from Yaser Rahmati

[root@academy tmp]#
  • To display the content of all the files in current directory:

[root@academy F1]# cat *
content of file1.txt
content of file2.txt
[root@academy F1]#
  • To put the output of a given file into another file:

[root@academy F1]# cat file2.txt
content of file2.txt
[root@academy F1]# cat file1.txt > file2.txt
[root@academy F1]# cat file2.txt
content of file1.txt
[root@academy F1]#
  • Append the contents of file1 to file2:

/[root@academy F1]# cat file1.txt
content of file1.txt
[root@academy F1]# cat file2.txt
content of file2.txt
[root@academy F1]# cat file1.txt >> file2.txt
[root@academy F1]# cat file2.txt
content of file2.txt
content of file1.txt
[root@academy F1]#
  • To concatenate two files together in a new file:

[root@academy F1]# cat file1.txt
content of file1.txt
[root@academy F1]# cat file2.txt
content of file2.txt
[root@academy F1]# cat file1.txt file2.txt >> merg.txt
[root@academy F1]# cat merg.txt
content of file1.txt
content of file2.txt
[root@academy F1]#
  • Some implementations of cat, with option -n, it's possible to show line numbers:

[root@academy F1]# cat myfile.txt
Line 1 : content 1
Line 2 : content 2
Line 3 : content 3
Line 4 : content 4
Line 5 : content 5
[root@academy F1]# cat -n myfile.txt
     1  Line 1 : content 1
     2  Line 2 : content 2
     3  Line 3 : content 3
     4  Line 4 : content 4
     5  Line 5 : content 5
[root@academy F1]#

Last updated