cat command

When using SSH to work on your site, you will often use the cat command. It is used to display file content on the screen. It is one of the most commonly used Linux commands to assist in troubleshooting.

Command: cat
Synopsis: cat [OPTION] [FILE]…

Options:
Below are the different options that are available to customize the cat command.

OptionLong NameDescription
-A–show-allEquivalent to running the v, E, and T options at the same time (-vET).
-b–number-nonblanknumber nonblank output lines
-eequivalent to running the v and E options together (-vE).
-E–show-endsdisplays $ at the end of each line.
-n–numbernumber all output lines
-s–squeeze-blankShow no more than a single blank line in a row.
-tequivalent to using v and T options together (-vT).
-T–show-tabsdisplay TAB characters as ^I
-v–show-nonprintinguse ^ and M- notation, except for LFD and TAB

Examples

Using the base cat command

This example uses the base cat command on a file named test.txt. It displays as it is configured in the file. Note that there are two blank lines after the first sentence.

$# cat test.txt Lorem ipsum dolor sit amet, consectetur adipiscing elit.   In convallis leo nec lectus accumsan, id vehicula dui tincidunt. Proin eget pellentesque lectus. Duis id mauris vel massa imperdiet egestas sit amet eget ante. Nulla at fermentum turpis.  Morbi eu dictum felis. Morbi vel ipsum nunc. Nunc ac sapien viverra, dignissim arcu vitae, gravida magna. Phasellus a adipiscing sapien. Cras vel aliquet massa. Mauris quis auctor eros. Ut gravida tellus at nisi facilisis, adipiscing cursus nisi luctus.

Removing extra blank lines

Using the squeeze (-s) option you see that there is only a single blank line displayed after the first sentence.

$# cat -s test.txt Lorem ipsum dolor sit amet, consectetur adipiscing elit.  In convallis leo nec lectus accumsan, id vehicula dui tincidunt. Proin eget pellentesque lectus. Duis id mauris vel massa imperdiet egestas sit amet eget ante. Nulla at fermentum turpis.  Morbi eu dictum felis. Morbi vel ipsum nunc. Nunc ac sapien viverra, dignissim arcu vitae, gravida magna. Phasellus a adipiscing sapien. Cras vel aliquet massa. Mauris quis auctor eros. Ut gravida tellus at nisi facilisis, adipiscing cursus nisi luctus.

View line numbers

If you wish to view the line numbers for the file, simply add the -n option.

$# cat -n test.txt      1  Lorem ipsum dolor sit amet, consectetur adipiscing elit.      2      3      4  In convallis leo nec lectus accumsan, id vehicula dui tincidunt. Proin eget pellentesque lectus. Duis id mauris vel massa      5  imperdiet egestas sit amet eget ante. Nulla at fermentum turpis.      6      7  Morbi eu dictum felis. Morbi vel ipsum nunc. Nunc ac sapien viverra, dignissim arcu vitae, gravida magna. Phasellus a      8  adipiscing sapien. Cras vel aliquet massa. Mauris quis auctor eros. Ut gravida tellus at nisi facilisis, adipiscing cursus nisi luctus.

Using multiple options

You can always place options together. In this example, we use the -s and the -n options together. This displays the line numbers as well as removing any extraneous blank lines.

$# cat -ns test.txt      1  Lorem ipsum dolor sit amet, consectetur adipiscing elit.      2      3  In convallis leo nec lectus accumsan, id vehicula dui tincidunt. Proin eget pellentesque lectus. Duis id mauris vel massa      4  imperdiet egestas sit amet eget ante. Nulla at fermentum turpis.      5      6  Morbi eu dictum felis. Morbi vel ipsum nunc. Nunc ac sapien viverra, dignissim arcu vitae, gravida magna. Phasellus a      7  adipiscing sapien. Cras vel aliquet massa. Mauris quis auctor eros. Ut gravida tellus at nisi facilisis, adipiscing cursus nisi luctus.

Was this article helpful? Join the conversation!