Skip to content

Both.org

News, Opinion, Tutorials, and Community for Linux Users and SysAdmins

Primary Menu
  • About Us
  • Computers 101
    • Hardware 101
    • Operating Systems 101
  • Linux
    • Why I use Linux
    • The real reason we use Linux
  • My Linux Books
    • systemd for Linux SysAdmins
    • Using and Administering Linux – Zero to SysAdmin: 2nd Edition
    • The Linux Philosophy for SysAdmins
    • Linux for Small Business Owners
    • Errata
      • Errata for The Linux Philosophy for SysAdmins
      • Errata for Using and Administering Linux — 1st Edition
      • Errata for Using and Administering Linux — 2nd Edition
  • Open Source Resources
    • What is Open Source?
    • What is Linux?
    • What is Open Source Software?
    • The Open Source Way
  • Write for us
    • Submission and Style guide
    • Advertising statement
  • Downloads
  • Home
  • Getting started with the Linux cat command
  • Linux

Getting started with the Linux cat command

Cat is a fairly simple tool designed to concatenate and write file(s) to your screen, which is known as standard output (stdout). The simplest use of cat is to show the contents of a file.
Alan Formy-Duval April 3, 2024 5 minutes read
A black cat holding a person's arm on a keyboard.

A black cat holding a person's arm on a keyboard.

1

Photo by Ruca Souza from Pexels

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

Closing thoughts

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.

Post navigation

Previous: Mastering Storage Management on Linux
Next: Using tar and ssh for backups

Related Stories

malware-289389409
  • Anti-Malware
  • Linux

ClamAV and Bleachbit: One two punch for Linux security

Don Watkins May 25, 2026
f44-01-night
  • Fedora
  • Linux
  • Opinion

Fedora 44 Impressions

David Both May 22, 2026
programming_code_keyboard_orange_hands
  • Linux
  • Programming

Converting tabs to spaces

Jim Hall May 20, 2026

Random Quote

If the input editor has been designed to reject all bad input, an ingenious idiot will discover a method to get bad data past it.

— Troutman’s Programming Postulates

Why I’ve Never Used Windows

On February 12 I gave a presentation at the Triangle Linux Users Group (TriLUG) about why I use Linux and why I’ve never used Windows.

Here’s the link to the video: https://www.youtube.com/live/uCK_haOXPFM 

Why there’s no such thing as AI

Last October at All Things Open (ATO) I was interviewed by Jason Hibbits of We Love Open Source. It’s posted in the article “Why today’s AI isn’t intelligent (yet)“.

Technically We Write — Our Partner Site

Our partner site, Technically We Write, has published a number of articles from several contributors to Both.org. Check them out.

Technically We Write is a community of technical writers, technical editors, copyeditors, web content writers, and all other roles in technical communication.

Subscribe to Both.org

To comment on articles, you must have an account.

Send your desired user ID, first and last name, and an email address for login (this must be the same email address used to register) to subscribe@both.org with “Subscribe” as the subject line.

You’ll receive a confirmation of your subscription with your initial password as soon as we are able to process it.

Administration

  • Log in
  • Entries feed
  • Comments feed
  • WordPress.org

License and AI Statements

Both.org aims to publish everything under a Creative Commons Attribution ShareAlike license. Some items may be published under a different license. You are responsible to verify permissions before reusing content from this website.

The opinions expressed are those of the individual authors, not Both.org.

You may not use this content to train AI.

 

Advertising Statement

Both.org does not sell advertising on this website.


Advertising may keep most websites running—but at Both.org, we’re committed to keeping our corner of the web ad-free. Both.org does not sell advertising on the website. Nor do we offer sponsored articles at this time. We’ll update this page if our position on sponsorships changes.

We want to be open about how the website is funded. Both.org is supported entirely by David Both and a few other dedicated individuals.

 

 

Copyright © All rights reserved. | MoreNews by AF themes.