Share this post
FaceBook  Twitter  
Contents[Hide]

1. Introduction

Many of you already know about the cat(1) command. Maybe you have already used it or even wrote scripts that used it. It's the kind of command that's small with a small number of options, yet popular and powerful at the same time. So don't expect a lengthy article here, but expect one about a powerful command that will save you from a lot of situations when you're at the command line. As usual, you should know that this is in no way a manual page substitute, so if you haven't installed the documentation already, do so now. cat(1) is part of the coreutils package, which is already installed 99.9% of the times, unless you have a really weird/geeky setup.

2. Basic usage and options

As with all Unix commands, cat also has a short but meaningful name. In our case, the originating word is concatenate. Merriam-Webster defines the word (the verb) as "to link together in a series or chain". The origin is the Latin word catena, which means "chain", and con, meaning "together". Now that we got etymology out of the way, let's see how the command works. It takes one or more files as arguments (and options, of course), and prints the result to standard output. The result being the two or more files' content together, one after the other, so the order of the arguments is important. If there's only one file, it simply prints the contents to stdout. So if I want to see my user's Vim config file page by page, I use cat, a pipe and less:

 $ cat ~/.vimrc | less 

In case you're wondering, there is concatenation taking place even in the single file example we just gave. That's because here cat concatenates .vimrc's contents to stdout, which is a file in its' own right, although not a regular one. Remember, in Unix everything is a file. Now let us say we want to add the contents of a Vim configuration file we found on the Internet to our existing one. But first, we want to see how it looks by saving the output to another file. Here's what we should do:

 $ cat .vimrc.example ~/.vimrc > .vimrc.new

If we like our resulting file, we can now copy it in place of the old one. After this basic usage introduction, here are the options with short explanations:

-A, --show-all - equivalent to -vET, meaning "show non-printing characters, while displaying '$' at the end of each line
and display tabs as ^I"
-b, --number-nonblank - number non-empty lines, overrides -n (see below)
-e - equals -vE (see below)
-E, --show-ends - display '$' at the end of each line
-n, --number - number all lines, see -b
-s, --squeeze-blank - if there are consecutive blank lines, squeeze them into one
-t - vT equivalent
-T, --show-tabs - display tabs as ^I, see -A
-u - ignored in GNU cat. In Solaris, for example, makes output unbuffered
-v, --show-nonprinting - show non-printing characters, except LFD (newline) and tabs
--help - obvious
--version - obvious

One important aspect of cat is that if used with no files, it takes stdin as an argument. Same if instead of a file, the '-' character is used, which is also used in other utilities offered by Linux in particular and Unix in general. So this command

 $ cat file1 - file2 

outputs the contents of file1, stdin (end with EOF or ctrl+d) and file2 to stdout.

3. Examples

Learning Linux cat command with examples
Linux command syntaxLinux command description
lynx -dump myarticle.html | cat -n
View my article with line numbers
cat myfile | command -arguments
Classic use of cat, by piping its output to other commands, like grep or tr
<file command -arguments
The same thing as above
cat myfile
Display myfile's contents to stdout
cat myfile yourfile
Add the contents of yourfile to myfile and output to stdout
cat myfile yourfile > ourfile
Add the contents of yourfile to myfile and write the result to ourfile
cat hisfile >> ourfile
Add the contents of hisfile to ourfile
cat ourfile | less
Display the contents of ourfile to stdout, one page at a time; this can be done like this: less ourfile
cat -t ourfile
Display all the contents of ourfile (except LFD) to stdout (useful with non-Unix text files - Mac, Windows)
cat ourfile | grep -i searchterm
Display only the line(s) that contain searchterm
cat -E ourfile
Display dollar signs at the end of each line
cat -A ourfile
Equivalent to -E and -t combined
cat -A -s ourfile
Display all contents of ourfile (except LFD) to stdout, printing dollar signs at the end of each line and squeezing multiple blank lines into one

Experienced Linux/Unix users know already that it's easy to overuse cat, even when you don't need it. The less example above is one of them , but this is also the case with sed or grep, to name just two. This won't mean that cat is actually useless, because it isn't, by no means. It's just knowing when to use it and when not to.

Partners

Who are we?

LinuxCareer.com is not affiliated with any local or international company, nor is it a recruitment or employment agency. We specialise in Linux based careers and closely related Information Technology fields by providing careers advice and latest employment opportunities.

JOIN LINUXCAREER

You can also get involved in the LinuxCareer project by participating on our FORUM or SUBMITTING A LINUX ARTICLE. We offer a range of privileges to our authors and good company.