cat – concatenate files and print to the standard output

cat is a simple yet very useful Unix command. It takes a name of one or more text files, and then shows their contents to the standard output as one stream of data. 

cat is used as the go-to text file viewer for most of small configuration or data files. If you know that the file is about 10-20 lines of code, cat would be the perfect tool to show its contents.

For larger files, more command or its improved version – less command – are recommended.

cat Unix command example

greys@ubuntu:~$ cat /etc/kernel-img.conf
do_symlinks = yes
relative_links = yes
do_bootloader = no
do_bootfloppy = no
do_initrd = yes
link_in_boot = no
postinst_hook = /sbin/update-grub
postrm_hook   = /sbin/update-grub

for two files, it looks like this:

greys@ubuntu:~$ cat /etc/issue
Ubuntu 7.04 \n \l
\
greys@ubuntu:~$ cat /etc/issue /etc/kernel-img.conf
Ubuntu 7.04 \n \l
\
do_symlinks = yes
relative_links = yes
do_bootloader = no
do_bootfloppy = no
do_initrd = yes
link_in_boot = no
postinst_hook = /sbin/update-grub
postrm_hook   = /sbin/update-grub

Line number in cat output

If you use the -n command line option with cat, your output will contain line numbers:

greys@ubuntu:~$ cat -n /etc/kernel-img.conf
     1  do_symlinks = yes
     2  relative_links = yes
     3  do_bootloader = no
     4  do_bootfloppy = no
     5  do_initrd = yes
     6  link_in_boot = no
     7  postinst_hook = /sbin/update-grub
     8  postrm_hook   = /sbin/update-grub

It is important to remember that -n numbers out the lines of the output generated by cat, not shows the number for each line in the original files. When working with more than one file, it’s easy to see what I mean:

greys@ubuntu:~$ cat -n /etc/issue /etc/kernel-img.conf
     1  Ubuntu 7.04 \n \l
     2
     3  do_symlinks = yes
     4  relative_links = yes
     5  do_bootloader = no
     6  do_bootfloppy = no
     7  do_initrd = yes
     8  link_in_boot = no
     9  postinst_hook = /sbin/update-grub
    10  postrm_hook   = /sbin/update-grub

While a line is a line, even if it’s empty, you could in some rare cases be only interested in numbering the lines which contain some information, and ignore the empty lines altogether. That’s when a -b option comes handy:

greys@ubuntu:~$ cat -bn /etc/issue /etc/kernel-img.conf
     1  Ubuntu 7.04 \n \l

     2  do_symlinks = yes
     3  relative_links = yes
     4  do_bootloader = no
     5  do_bootfloppy = no
     6  do_initrd = yes
     7  link_in_boot = no
     8  postinst_hook = /sbin/update-grub
     9  postrm_hook   = /sbin/update-grub

See also