cat

October 1st, 1997 by Patrick Hill in

Here's a spiffy little command that can be used to combine files, look at the contents of a file and do limited text editing.
Your rating: None Average: 4.8 (4 votes)

The Linux cat command at first seems so simple as to be unnecessary. In actuality, it is an excellent example of the Unix philosophy: create programs which do one thing and do it well. The thing cat does well is display the contents of a file or files. Many other utilities can handle this task, but none have all the options cat does. First, let's look at the simplest case:

cat /etc/motd
This command will display the contents of the motd (messages of the day) file to your screen. Unlike more (or less), cat will not stop when the screen is full. This is a feature, not a bug. You don't want pauses when redirecting (using the > operator) cat's output to a device, e.g., a printer or modem:
cat /etc/motd > /dev/modem
cat comes from the word conCATenate, which describes one of its best uses: to concatenate or “glue together” two or more files. If you have several individual files about animals you would like to collect together into one file, cat will do the work for you. For example:
cat tiger lion cougar > bigcats
would redirect the concatenation output, containing the three feline files in the specified order, into a new file named bigcats. If you find another file, panther, that needs to be added to the bigcats file, use cat with the append (>>) operator in the following way:
cat panther >> bigcats
Using >> ensures any prior content of bigcats is preserved. The content of panther is appended to bigcats. If you were to use the > operator here, you would replace the contents of bigcats with the contents of panther. Always use >> when you wish to add to the end of an existing file.

Be careful not to use the same file name when redirecting the output of the cat command, or you could lose one of the files. For example, don't do the following:

cat myfile yourfile > yourfile

In this case yourfile gets overwritten by myfile.

Another surprisingly handy use for cat is to redirect standard input like this:

cat > newfile
some notes I want to save in newfile.
CTRL-D

This creates a new file (named newfile). You type as much text as you wish, then type ctrl-D to save the file. You can backspace over mistakes, but you cannot go to a previous line after you press the enter key. I often teach this particular option of cat to novice Unix users, who occasionally need to create simple files, but don't want to learn vi or other simple editors. There may be Unix systems without vi or your favorite editor, but cat is always there.

The operator >> can also be used to append notes to the end of newfile:

cat >> newfile
Adding another note to newfile.
CTRL-D
cat Switches

Like most Unix commands, the behavior of cat can be modified by command line switches. If you use the diff command to compare files, it will show you the numbers of lines that differ between the files. However, most files don't have line numbers. Use cat with the -n switch to number each line of a file:

cat -n kittens > num_kittens

The file num_kittens is kittens with a number in front of each line, including blank lines. Use the -b switch to number only lines that are not blank.

One last cat trick: using the -v switch will show you “hidden” characters, such as control characters that may not show up in your editor. Try this experiment:

cat > catestv
CTRL-v testing CTRL-O Testing esc-b
CTRL-D

If we use cat to view the file, we see only the normal text:

cat catestv
Testing Testing
To see what's actually in the file, use the -v option:
cat -v catestv
^V Testing ^O Testing ^[b
Here, a ^ in front of a character signifies a control character. (CTRL-[ is the same as ESC).

Patrick Hill (apathos@bham.net) is a computer engineer at Alabama Power Company in Birmingham, Alabama. He is known around the office as the guy who uses cat for an editor.

__________________________


Special Magazine Offer -- Free Gift with Subscription
Receive a free digital copy of Linux Journal's System Administration Special Edition as well as instant online access to current and past issues. CLICK HERE for offer

Linux Journal: delivering readers the advice and inspiration they need to get the most out of their Linux systems since 1994.

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
Anonymous's picture

1997...?

On September 8th, 2009 Anonymous (not verified) says:

1997? Is this correct?
The time stamp of the posting says: "October 1st, 1997 by Patrick Hill"

That's only... 12 years ago! :x

Dif's picture

No, I just posted this in

On October 9th, 2009 Dif (not verified) says:

No, I just posted this in September of 2009

Dig's picture

Using Cat

On September 2nd, 2009 Dig (not verified) says:

Hi,

I have used the cat command to put the contents of a large number of files into one large file. So I have each files contents in this large file without a blank line between the files. How can I add the filename before the contents of the file and a blank line between the files in my large output file?

Dig's picture

Using Cat

On September 3rd, 2009 Dig (not verified) says:

OK, I have all the filenames showing before the contents and with a carriage return after the filename. I just need to know how to add a carriage return after each files contents. Here is the code.

#!/bin/sh

# no error checking
rm -f newfile
for i in *.spc;
do printf "%s\n" $i >> newfile
cat "$i" >> newfile
done

imtiyaz hussain's picture

To print specific range of lines using cat

On August 31st, 2009 imtiyaz hussain (not verified) says:

Hello Patrick,

I am using AIX Unix. How is it possible to select lines to be printed using cat ? If the file size is very big and say it has more than thousand lines and i want to print lines 65 to 70 on standard output.

Regards,
Immi.

Anonymous's picture

Re: Take Command: cat

On May 29th, 2003 Anonymous says:

Good examples. Something puzzles me though. I am taking a Unix class using Solaris 2.8 on Sun workstations, but I do my practicing on a PC with Linux (RedHat 8). In Unix you can create a new file with:

cat fileName
blah blah
Ctrl-D

But with Linux this produces a "no such file" error, so you must explicitely redirect the output with:

cat > fileName ...

I have tried it with all of the shells installed and get the same. I wonder why the difference?

Anonymous's picture

Re: Take Command: cat

On January 5th, 2002 Anonymous says:

i am very thankful for the detailed information

of the cat command. many of the linux sites

are not having the basic information of cat

command which is must. i pity the negligence

attributed by the linux site maintainers.

any how i am thankful for the detailed and

friendly information.

thanking you

Post new comment

Please note that comments may not appear immediately, so there is no need to repost your comment.
The content of this field is kept private and will not be shown publicly.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <pre> <ul> <ol> <li> <dl> <dt> <dd> <i> <b>
  • Lines and paragraphs break automatically.

More information about formatting options

Newsletter

Each week Linux Journal editors will tell you what's hot in the world of Linux. You will receive late breaking news, technical tips and tricks, and links to in-depth stories featured on www.linuxjournal.com.
Sign up for our Email Newsletter

Tech Tip Videos

From the Magazine

December 2009, #188

If last month's Infrastrucuture issue was too "big" for you then try on this month's Embedded issue. Find out how to use Player for programming mobile robots, build a humidity controller for your root cellar, find out how to reduce the boot time of your embedded system, and if you're new to embedded systems find out the basics that go into one. You can also read about the Beagle Board, the Mesh Potato and a spate of other interestingly named items. And along with our regular columns don't miss our new monthly column: Economy Size Geek.







Read this issue