How to Use the Linux Cat Command

Display and combine files from a shell prompt

Red Ginger Cat on Computer Keyboard

Valery Kudryavtsev / Getty Images 

The cat command in Linux concatenate files and displays the output to the standard output (usually, the shell).

One of the most common uses of cat is to display a file to the screen and also to create a file on the fly and allow basic editing straight at the terminal.

How to Create a File Using 'cat'

To create a file using the cat command enter the following in the terminal window:

cat > filename

When you create a file in this manner, the cursor will be left on a new line, and you can start typing. This technique offers a great way to start a text file. To finish editing the file, press Ctrl+D. The file saves with whatever you used for filename.

Test that the process worked by typing the ls command:

ls -lt

You should see your new file, and the size should be greater than zero.

How to Display a File Using Cat

The cat command displays a file to the screen as well. All you need to do is eliminate the greater than symbol as follows:

cat <nameoffile>

To view the file page by page use the more command:

cat <nameoffile> | more

Alternatively, you can use the less command as well:

cat <nameoffile> | less

How to Show Line Numbers

For all the non-empty lines in a file use the following command:

cat -b <nameoffile>

If there are lines with no characters at all they won't be numbered. To show numbers for all the lines regardless as to whether they are blank, type the following command:

cat -n <nameoffile>

How to Show the End of Each Line

Sometimes when parsing data files, programmers discover problems because there are hidden characters at the end of lines that they weren't expecting—such as spaces. This error prevents their parsers from working correctly.

To show the dollar as an end of line character enter the following command:

cat -E <nameoffile>

As an example look at the following line of text

the cat sat on the mat

 When you run this with the cat -E command you receive the following output:

the cat sat on the mat$

Reducing Blank Lines

When you show the contents of a file using the cat command you probably don't want to see when there are loads of consecutive blank lines. Use the -s switch to condense all blank lines into a single blank line:

cat -s <nameoffile>

How to Show Tabs

When you display a file that uses tab delimiters, you won't ordinarily see the tabs.

The following command shows ^I instead of the tab, which makes it easy to see them:

cat -T <nameoffile>

Concatenate Multiple Files

The whole point of cat is concatenation. Concatenate several files to the screen with the following command:

cat <nameoffile1> <nameoffile2>

To concatenate the files and create a new file use the following command:

cat <nameoffile1> <nameoffile2> > <newfile>

Showing Files in Reverse Order

Show a file in reverse order by using the following command:

tac <nameoffile>

T technically this isn't the cat command, it is the tac command, but it essentially does the same thing but in reverse.