Linux cat command examples

The Linux cat command means "concatenate and print files". Usually all I use it for is to display a file's contents, like this command that displays the contents of a file named "":


With this cat command example, the contents of my small shell script ("") are displayed on the screen as fast as the system can display them. If the file is only a few lines it will probably be shown on your terminal without scrolling, but if it's a large file it will scroll, and scroll, and scroll.

In my case the file is small, so the command and output look like this next cat example:

prompt> cat -n 
for i in `ls *jpg`
  mogrify -quality 60 $i

An interesting cat command option is -n, which puts line numbers on your file. In this next sequence I again "cat out" my small shell script, this time with the -n option:

prompt> cat -n 
1  for i in `ls *jpg`
2  do
3    mogrify -quality 60 $i
4  done

Being able to show line numbers when displaying file contents with cat is a pretty cool feature.

The -v option is also seems cool, it displays non-printing characters. (I have used that option before, so if it doesn't work, this sed script will also work.)

Cat example - working with contents from multiple files

The other very common way I use the cat command is to merge several files into one larger file, like this:

cat file1 file2 file3 > big_file

After running that command the file "big_file" contains the merged contents of the files "file1", "file2", and "file3".

Related links