Cat is a fairly simple tool designed to concatenate and write file(s) to your screen, which is known as standard output (stdout). It is part of the GNU Core Utils released under the GPLv3+ license. You can expect to find it in just about any Linux distribution or other Unix operating environment, such as FreeBSD or Solaris. The simplest use of cat is to show the contents of a file. Here is an example with a file named hello.world:
$ ls
hello.world
$ cat hello.world
Hello World!
$
The most common way I use the cat command is for viewing configuration files, such as those in the /etc directory. The cat command will display a file without risking damage to it. If I open a critical configuration file using an editor such as Vi or Nano, I could inadvertently make unwanted changes to the file. The cat command is not an editor and therefore poses no risk of making changes to a file's content.
If I need to view a longer file, I can use a pipe with the more command:
$ cat <somelongfile> | more
Cat can display multiple files at the same time. If we want to see two files—hello.world and goodbye.world—we would include both filenames as arguments in the command:
$ cat hello.world goodbye.world
Hello World!
Good Bye World!
$
Cat can also number a file's lines during output. There are two commands to do this, as shown in the help documentation:
-b, --number-nonblank number nonempty output lines, overrides -n
-n, --number number all output lines
If I use the -b command with the hello.world file, the output will be numbered like this:
$ cat -b hello.world
1 Hello World!
$
In the example above, there is an empty line. We can determine why this empty line appears by using the -n argument:
$ cat -n hello.world
1 Hello World!
2
$
Now we see that there is an extra empty line. These two arguments are operating on the final output rather than the file contents, so if we were to use the -n option with both files, numbering will count lines as follows:
$ cat -n hello.world goodbye.world
1 Hello World!
2
3 Good Bye World!
4
$
One other option that can be useful is -s for squeeze-blank. This argument tells cat to reduce repeated empty line output down to one line. This is helpful when reviewing files that have a lot of empty lines, because it effectively fits more text on the screen. Suppose I have a file with three lines that are spaced apart by several empty lines, such as in this example, greetings.world:
$ cat greetings.world
Greetings World!
Take me to your Leader!
We Come in Peace!
$
Using the -s option saves screen space:
$ cat -s greetings.world
Greetings World!
Take me to your Leader!
We Come in Peace!
$
Cat is often used to copy contents of one file to another file. You may be asking, "Why not just use cp?" Here is how I could create a new file, called both.files, that contains the contents of the hello and goodbye files:
$ cat hello.world goodbye.world > both.files
$ cat both.files
Hello World!
Good Bye World!
$
zcat
There is another variation on the cat command known as zcat. This command is capable of displaying files that have been compressed with Gzip without needing to uncompress the files with the gunzip command. As an aside, this also preserves disk space, which is the entire reason files are compressed!
The zcat command is a bit more exciting because it can be a huge time saver for system administrators who spend a lot of time reviewing system log files. Where can we find compressed log files? Take a look at /var/log on most Linux systems. On my system, /var/log contains several files, such as syslog.2.gz and syslog.3.gz. These files are the result of the log management system, which rotates and compresses log files to save disk space and prevent logs from growing to unmanageable file sizes. Without zcat, I would have to uncompress these files with the gunzip command before viewing them. Thankfully, I can use zcat:
$ cd /var/log
$ ls *.gz
syslog.2.gz syslog.3.gz
$
$ zcat syslog.2.gz |more
Jan 30 00:02:26 workstation systemd[1850]: Starting GNOME Terminal Server...
Jan 30 00:02:26 workstation dbus-daemon[1920]: [session uid=2112 pid=1920] Successful
ly activated service 'org.gnome.Terminal'
Jan 30 00:02:26 workstation systemd[1850]: Started GNOME Terminal Server.
Jan 30 00:02:26 workstation org.gnome.Terminal.desktop[2059]: # watch_fast: "/org/gno
me/terminal/legacy/" (establishing: 0, active: 0)
Jan 30 00:02:26 workstation org.gnome.Terminal.desktop[2059]: # unwatch_fast: "/org/g
nome/terminal/legacy/" (active: 0, establishing: 1)
Jan 30 00:02:26 workstation org.gnome.Terminal.desktop[2059]: # watch_established: "/
org/gnome/terminal/legacy/" (establishing: 0)
--More--
We can also pass both files to zcat if we want to review both of them uninterrupted. Due to how log rotation works, you need to pass the filenames in reverse order to preserve the chronological order of the log contents:
$ ls -l *.gz
-rw-r----- 1 syslog adm 196383 Jan 31 00:00 syslog.2.gz
-rw-r----- 1 syslog adm 1137176 Jan 30 00:00 syslog.3.gz
$ zcat syslog.3.gz syslog.2.gz |more
The cat command seems simple but is very useful. I use it regularly. You also don't need to feed or pet it like a real cat. As always, I suggest you review the man pages (man cat) for the cat and zcat commands to learn more about how it can be used. You can also use the --help argument for a quick synopsis of command line arguments.
6 Comments