Cat command is one of the basic commands that you learned when you started in the Unix / Linux world.
You already know that cat displays a file content. What more could this command do?
This tutorial gives 10 practical usage examples for cat command. Probably few of these examples could be new to you. Or, it might just be a refresher for you, if you already knew these examples.
1. Display the contents of a file
When you pass the filename as an argument to cat, it displays the contents of the file as shown below.
$ cat program.pl #!/usr/bin/perl if( 2 ge 3) { print "greater\n"; }else { print "lesser\n"; }
You can also display contents of more than one file as shown below.
$ cat program.pl program2.pl #!/usr/bin/perl if( 2 ge 3) { print "greater\n"; }else { print "lesser\n"; } #!/usr/bin/perl @arr = qw(1 2 3); $ref = \@arr; print ref $ref;
2. Create a New File
Using cat command, the lines received from stdin can be redirected to a new file using redirection symbols.
When you type simply cat command without any arguments, it just receives the stdin content and displays it in the stdout. So after typed the line once, when you press enter, the same line gets printed in the subsequent line as seen below.
$ cat cat command for file oriented operations. cat command for file oriented operations. cp command for copy files or directories. cp command for copy files or directories.
You can also redirect the stdout to a new file as shown below.
$ cat >cmd_usage.txt cat command for file oriented operations. cp command for copy files or directories. $ cat cmd_usage.txt cat command for file oriented operations. cp command for copy files or directories.
Sometimes you may have to append the content to a file, use >> redirection symbol as shown below.
$ cat >>cmd_usage.txt ls command to list out file and directory with its attributes. $ cat cmd_usage.txt cat command for file oriented operations. cp command for copy files or directories. ls command to list out file and directory with its attributes.
3. Copy File Content
Redirection symbols in unix plays an important role in processing the standard file descriptor contents. Using it, you can copy the contents of one file into another as shown below.
$ cat program.pl >backup_prgm.pl
As seen above, since we used the output redirection, the content displayed in standard output gets directed into a new file called backup_pgrm.pl. View the contents of backup_pgrm.pl:
$ cat backup_pgrm.pl #!/usr/bin/perl if( 2 ge 3) { print "greater\n"; }else { print "lesser\n"; }
4. Concatenate Contents of Multiple Files
Through cat command, you will be able to concatenate contents of more than one file into a new file.
For example, the codes from program.pl and program2.pl gets combined into a new file all_pgrm.pl.
$ cat program.pl program2.pl >all_pgrm.pl
As seen above, stdout gets redirected and the new file has been created with the contents of program.pl and program2.pl. Verify the contents of all_pgrm.pl:
$ cat all_pgrm.pl #!/usr/bin/perl if( 2 ge 3) { print "greater\n"; }else { print "lesser\n"; } #!/usr/bin/perl @arr = qw(1 2 3); $ref = \@arr; print ref $ref;
5. Display Line numbers
To display the contents of a file with the line number in front of each line, use option -n. The following example, prints the line number for the lines from program.pl,
$ cat -n program.pl 1 #!/usr/bin/perl 2 3 4 if( 2 ge 3) { 5 print "greater\n"; 6 } else { 7 print "lesser\n"; 8 } 9 10
As you noticed above, even the empty lines are numbered. In case of numbering only nonempty lines, use option -b as follows,
$ cat -b program.pl 1 #!/usr/bin/perl 2 3 if( 2 ge 3) { 4 print "greater\n"; 5 } else { 6 print "lesser\n"; 7 }
Note that the lines which contains whitespaces are not considered as empty lines and the same applicable to line numbered 2.
6. Concatenate File Contents along with Input from Stdin
There is a possibility to read lines from stdin along with concatenation of other files. Hence the user can type his own content whenever its required.
In the following example, you can insert a few lines (from stdin) in the beginning while combining files together.
$ cat - program.pl program2.pl >all_pgrm.pl Contents from file : program.pl, program2.pl
As seen above, – is the place where you can read from stdin, accordingly 1 line from stdin has been inserted into the beginning of a new file called all_pgrm.pl with the latter contents from program.pl and program2.pl files:
$ cat -n all_pgrm.pl 1 Contents from file : program.pl, program2.pl 2 #!/usr/bin/perl 3 4 5 if( 2 ge 3) { 6 print "greater\n"; 7 } else { 8 print "lesser\n"; 9 } 10 11 12 #!/usr/bin/perl 13 14 @arr = qw(1 2 3); 15 $ref = \@arr; 16 print ref $ref;
7. Don’t Display Repeated Empty Output Lines
Sometimes the file would contain repeated empty lines which you don’t want to display in the stdout while listing it out. cat command provides an option called -s which will suppress consecutive empty output lines into one and displays.
As noticed in the first example of usage 5 (i.e: Display with line number infront of each lines), there is two consecutive empty output lines in the file program.pl numbered 9 and 10. May be you don’t want to display those repeated empty output lines. This can be suppressed as shown below:
# cat -sn program.pl 1 #!/usr/bin/perl 2 3 4 if( 2 ge 3) { 5 print "greater\n"; 6 } else { 7 print "lesser\n"; 8 } 9
Respectively the line 9 and 10 gets suppressed into one empty line in the above output (i.e:line 9).
8. Display End of Line and TAB characters
You can make the cat to display the $ character at end of every line. Normally by listing file contents, users cant identify whitespaces at the end of each lines, by using the cat -e option.
For instance, use -e option on the file program.pl. As shown below, the third line of this file (i.e:program.pl) is actually not an empty line and as well the line 7 is ending with whitespaces respectively.
$ cat -ne program.pl 1 #!/usr/bin/perl$ 2 $ 3 $ 4 if( 2 ge 3) {$ 5 print "greater\n";$ 6 } else {$ 7 print "lesser\n"; $ 8 }$ 9 $ 10 $
Use option -T to display the tab characters. It displays ^I for TAB character. As shown below, line5 and line7 starts with a TAB character.
$ cat -neT program.pl 1 #!/usr/bin/perl$ 2 $ 3 $ 4 if( 2 ge 3) {$ 5 ^Iprint "greater\n";$ 6 } else {$ 7 ^Iprint "lesser\n"; $ 8 }$ 9 $ 10 $
9. Read Content until a Specific Pattern
The here document can be used along with cat command. For example, when you are reading from stdin, you can read until a line that contains a specific pattern. In the example below, the block of lines are read from stdin (until EOF) and printed on the standard output.
$ cat <<EOF > mv command to move files and directories > top command to display linux tasks > EOF mv command to move files and directories top command to display linux tasks
10. Display File Content in Reverse
This example is a cheater. This is really not a cat command example, but it is related.
tac is the reverse of cat. As you can imagine, tac will just display the contents of a file in reverse order (lines from bottom is displayed first). If you just want to reverse the characters in the line, you should use rev command.
For example, the file program.pl is being displayed in reverse as:
$ tac program.pl } print "lesser\n"; } else { print "greater\n"; if( 2 ge 3) { #!/usr/bin/perl
Comments on this entry are closed.
Hi,
Thanks a lot,
simple and nice article…
Actually, #9 (Read Content until a Specific Pattern) is not a feature of the cat command, rather a feature of the shell. This is useful to know because it can be used for any other command that can take input this way. See below (using /bin/zsh in my case but this applies to most other shells too):
% tail -n 1 < Line 1
heredoc> Line 2
heredoc> EOF
Line 2
%
Re: “Use option -T to display the tab characters. It displays ^I for TAB character. As shown below, line5 and line6 starts with a TAB character.”
Actually it appears you meant line5 and line7 😉
I really appreciate your tutorial!
This may be a minor nitpick, but in the example program2.pl you are using the following line:
@arr = qw(1,2,3);
It builds an array with just one element, the string: “1,2,3”
That happens, because the qw-Operator expects a list of whitespace separated elements. To construct an array with three elements you either need to omit the qw-Operator or the commata:
@arr = (1, 2, 3);
or
@arr = qw(1 2 3);
Since this is a typical newbie error i wanted to point it out.
When I saw the title first time, I thought what would be the extra unknown options. But yes, there are many unknown options even in cat command.
and
Most of the options to cat are GNU specific; non-Linux implementations will probably not have them.
The only option in the POSIX specification for cat is one you don’t mention: -u.
cat -v is also useful. Especially if your file has some obscure characters in it, or some command like characters that are interpreted at binary at the start of the file
New to The Geek Stuff…love it! Thanks for the article.
Good content….esp the last command tac is of fun….
@Gaylen, @Legan
Thanks for pointing out the issue. It is fixed now.
Learning something new everyday. Thanks
Cat can also be the poor man’s dd. dd gives you lots of power, but if you just want (say) to copy a DVD to an .ISO file, cat will do it for you handily:
cat /dev/dvd > dvd_image.iso
cat -v comes in handy if you are suspecting CRLF endings 🙂 it will show ^M at the end of each line.
doc@desktop:~/tmp$ cat > testfile<<<"hi there"
doc@desktop:~/tmp$ unix2dos testfile
unix2dos: converting file testfile to DOS format …
doc@desktop:~/tmp$ cat -v testfile
hi there^M
doc@desktop:~/tmp$ dos2unix testfile
dos2unix: converting file testfile to Unix format …
doc@desktop:~/tmp$ cat -v testfile
hi there
Nice tips. But… what do I have to type to stop input from stdin? (ex. #6) If I make Ctrl+C, it stops without concatenating the files’ contents so ther’s only text from stdin. Thanks. Mike
KT, Ctrl+D
I don’t want to display the content of the file, because i’m using the command strstr in read.c just for show the cache. Then, when I use ‘cat’, the content of the file overwrites the status of the cache, can someone help me ?
Sorry for the poor english !
Write a program that monitors a directory and maintains a log of it’s
contents. Could anybody help me out solving this program?
let say i want to print the frist 100 lines ending with AC in a fasta file. how do i do that.
While I have no idea what a “fasta file” is, the way to find the first 100 lines ending with the letters ‘AC’ would be like this:
grep “AC$” filenamehere.txt | head -100