Exploring the "cat" command

By Alvin J. Alexander, devdaily.com

The Unix cat command means "concatenate and print files". Usually all I use it for is to display a file's contents, like this command to display the contents of a file named "lighten-images.sh":

cat lighten-images.sh

With this command the contents of my small shell script ("lighten-images.sh") 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:

prompt> cat -n lighten-images.sh 
for i in `ls *jpg`
do
  mogrify -quality 60 $i
done

An interesting 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 lighten-images.sh 
1  for i in `ls *jpg`
2  do
3    mogrify -quality 60 $i
4  done

It's pretty nice to be able to show line numbers like that.

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.)

Catting out 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


devdaily logo