cat, head and tail Command in Linux with Examples
Linux cat command
We have been using the cat command to simply display the contents of files. Usually, the cat command generates a faithful copy of its input, without performing any edits or conversions. When called with one of the following command line switches, however, the cat command will indicate the presence tabs, line feeds, and other control sequences, using the following conventions.
Command Line Switches for the cat Command
Switch | Effect |
---|---|
-E | display line feeds (ASCII 10) as $ |
-T | display tabs (ASCII 9) as ^I |
-v | display whitespace and control characters as ^n, with n indicating the CTRL sequence for the nonprinting character. |
-A | Shows “all”, same as -vET |
-t | Show “all” except line feeds, same as -vT |
-e | Show “all” except tabs, same as -vE |
As an example, in the following, the cat command is used to display the contents of the /etc/hosts configuration file.
$ cat /etc/hosts
# Do not remove the following line, or various programs
# that require network functionality will fail.
127.0.0.1 localhost.localdomain localhost station.example.com
127.0.0.1 rha-server
192.168.0.1 station1 station1.example.com www1 www1.example.com
192.168.0.51 station51 station51.example.com
192.168.129.201 z
Using the -A command line switch, the whitespace structure of the file becomes evident, as tabs are replaced with ^I, and line feeds are decorated with $.
$ cat -A /etc/hosts
# Do not remove the following line, or various programs$
# that require network functionality will fail.$
127.0.0.1^Ilocalhost.localdomain^Ilocalhost station.example.com $
127.0.0.1^Irha-server$
192.168.0.1^Istation1 station1.example.com www1 www1.example.com$
192.168.0.51^Istation51 station51.example.com$
192.168.129.201^Iz$
Linux head and tail commands
The head and tail commands have been used to display the first or last few lines of a file, respectively. But what makes a line? Imagine yourself working at a typewriter: click! clack! click! clack! clack! ziiing! Instead of the ziing! of the typewriter carriage at the end of each line, the line feed character (ASCII 10) is chosen to mark the end of lines.
Unfortunately, a common convention for how to mark the end of a line is not shared among the dominant operating systems in use today. Linux (and Unix) uses the line feed character (ASCII 10, often represented \n), while Macintosh operating systems uses the carriage return character (ASCII 13, often represented \r or ^M), and Microsoft operating systems use a carriage return/line feed pair (ASCII 13, ASCII 10).
For example, the following file contains a list of four musicians.
$ cat -A musicians
elvis$
blondie$
prince$
madonna$
Had this file been created on a Microsoft or Macintosh operating system, and copied into Linux, the files would look like the following.
$ cat -A musicians.dos
elvis^M$
blondie^M$
prince^M$
madonna^M$
$ cat -A musicians.mac
elvis^Mblondie^Mprince^Mmadonna^M$
Linux (and Unix) text files generally adhere to a convention that the last character of the file must be a line feed for the last line of text. Following the cat of the file musicians.mac, which does not contain any conventional Linux line feed characters, the bash prompt is not displayed in its usual location.
Command Line Switches for the head Command
Switch | Effect |
---|---|
-N, -nN | Display the first N lines of the file. |
-cN | Display the first N bytes of the file. |
Command Line Switches for the tail Command
Switch | Effect |
---|---|
-N, -nN | Display the last N lines of the file. If N is prepended by a +, display the remainder of the file, starting at the Nth line. |
-cN | Display the first N bytes of the file. |