Science and technology

Getting began with the cat command

Cat is a reasonably easy device designed to concatenate and write file(s) to your display screen, which is called normal output (stdout). It is a part of the GNU Core Utils launched underneath the GPLv3+ license. You can anticipate finding it in nearly any Linux distribution or different Unix working surroundings, corresponding to FreeBSD or Solaris. The easiest use of cat is to indicate the contents of a file. Here is an instance with a file named whats up.world:

$ ls
whats up.world
$ cat whats up.world
Hello World!

$

The most typical means I take advantage of the cat command is for viewing configuration recordsdata, corresponding to these within the /and so on listing. The cat command will show a file with out risking injury to it. If I open a essential configuration file utilizing an editor corresponding to Vi or Nano, I may inadvertently make undesirable modifications to the file. The cat command isn’t an editor and subsequently poses no danger of constructing modifications to a file’s content material.

If I must view an extended file, I can use a pipe with the extra command:

$ cat <somelongfile> | extra

Cat can show a number of recordsdata on the identical time. If we wish to see two recordsdata—whats up.world and goodbye.world—we would come with each filenames as arguments within the command:

$ cat whats up.world goodbye.world
Hello World!

Good Bye World!

$

Cat may quantity a file’s strains throughout output. There are two instructions to do that, as proven within the assist documentation:

-b, --number-nonblank    quantity nonempty output strains, overrides -n
-n, --number             quantity all output strains

If I take advantage of the -b command with the whats up.world file, the output might be numbered like this:

$ cat -b whats up.world
     1  Hello World!

$

In the instance above, there may be an empty line. We can decide why this empty line seems by utilizing the -n argument:

$ cat -n whats up.world
     1  Hello World!
     2
$

Now we see that there’s an additional empty line. These two arguments are working on the ultimate output reasonably than the file contents, so if we have been to make use of the -n choice with each recordsdata, numbering will depend strains as follows:

$ cat -n whats up.world goodbye.world
     1  Hello World!
     2 
     three  Good Bye World!
     four
$

One different choice that may be helpful is -s for squeeze-blank. This argument tells cat to scale back repeated empty line output down to at least one line. This is useful when reviewing recordsdata which have numerous empty strains, as a result of it successfully suits extra textual content on the display screen. Suppose I’ve a file with three strains which are spaced aside by a number of empty strains, corresponding to on this instance, greetings.world:

$ cat greetings.world
Greetings World!

Take me to your Leader!

We Come in Peace!
$

Using the -s choice saves display screen area:

$ cat -s greetings.world
Greetings World!

Take me to your Leader!

We Come in Peace!
$

Cat is usually used to repeat contents of 1 file to a different file. You could also be asking, “Why not simply use cp?” Here is how I may create a brand new file, known as each.recordsdata, that comprises the contents of the whats up and goodbye recordsdata:

$ cat whats up.world goodbye.world > each.recordsdata
$ cat each.recordsdata
Hello World!

Good Bye World!

$

zcat

There is one other variation on the cat command often called zcat. This command is able to displaying recordsdata which have been compressed with Gzip with no need to uncompress the recordsdata with the gunzip command. As an apart, this additionally preserves disk area, which is your complete motive recordsdata are compressed!

The zcat command is a little more thrilling as a result of it may be an enormous time saver for system directors who spend numerous time reviewing system log recordsdata. Where can we discover compressed log recordsdata? Take a have a look at /var/log on most Linux techniques. On my system, /var/log comprises a number of recordsdata, corresponding to syslog.2.gz and syslog.three.gz. These recordsdata are the results of the log administration system, which rotates and compresses log recordsdata to save lots of disk area and stop logs from rising to unmanageable file sizes. Without zcat, I must uncompress these recordsdata with the gunzip command earlier than viewing them. Thankfully, I can use zcat:

$ cd /var/log
$ ls *.gz
syslog.2.gz  syslog.three.gz
$
$ zcat syslog.2.gz |extra
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: zero, energetic: zero)
Jan 30 00:02:26 workstation org.gnome.Terminal.desktop[2059]: # unwatch_fast: "
/org/g
nome/terminal/legacy/" (energetic: zero, establishing: 1)
Jan 30 00:02:26 workstation org.gnome.Terminal.desktop[2059]: # watch_established: "
/
org/gnome/terminal/legacy/" (establishing: zero)
--More--

We may go each recordsdata to zcat if we wish to assessment each of them uninterrupted. Due to how log rotation works, that you must go the filenames in reverse order to protect 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.three.gz
$ zcat syslog.three.gz syslog.2.gz |extra

The cat command appears easy however could be very helpful. I take advantage of it commonly. You additionally needn’t feed or pet it like an actual cat. As all the time, I counsel you assessment the person pages (man cat) for the cat and zcat instructions to study extra about how it may be used. You may use the –help argument for a fast synopsis of command line arguments.

Most Popular

To Top