16 cat Command Examples in Linux (The Complete Guide)

Today I have brought a handy command for you, and that is the cat command.

cat‘ stands for “Concatenate“.

Whether you are a beginner or an expert in Linux, this is the most frequently used command for all.

Key features of cat command:

  • Create a File
  • Concatenate Files
  • Combine Binary files
  • Display contents of a File
  • cat command can be used in conjunction with other commands such as head, tail, more, less.
  • Print Files
  • Can display useful system information such as CPU Information, Memory Information, etc.

The cat command was launched in the Unix operating system and was written by Torbjorn Granlund and Richard M. Stallman.

In this article, I will teach you about the complete features of cat command.

First of all, let’s focus on some of the most important options that we can use with the cat.

OptionsExplanation
-A, --show-allEquivalent to -vET
-b, --number-nonblankNumber nonempty output lines
-eEquivalent to -vE
-EDisplay $ at end of each line
-nNumber all output lines
-sSuppress repeated empty output lines
-tEquivalent to -vT
-TDisplay TAB characters as ^I
-vShow nonprinting i.e. use ^ and M- notation, except for LFD and TAB.
--helpDisplay help page of cat command.
--versionPrint version information

We can use the following Redirection operators with cat command:

  • > : Redirection of output
  • >> : Appends output to the specified file
  • < : Redirection of input
  • | : Pipe

Syntax:

You must follow the syntax given below to use the cat command.

cat [OPTION]... [FILE]...

1. Create a New file

To create a new file, you must combine the Redirection of output (>) operator with the cat.

Syntax:

~$ cat > [File Name]

Refer to the following example.

~$ cat > test.txt

After running the above command, it will allow you to type the text you want to store in the test.txt file.

After typing, press the CTRL + D (Hold the CTRL button and then press D) button on your keyword to save the file.

Create a new file using cat command

2. Display contents of a File

You can display the contents of a file using cat.

To do this type the following command.

~$ cat week.txt 
Monday
Tuesday
Wedneswday
Thursday
Friday
Saturday
Sunday

OR you can mention the path of the file.

~$ cat data/file1.txt 
root:x:0:0:root:/root:/bin/bash
daemon:x:1:1:daemon:/usr/sbin:/usr/sbin/nologin
bin:x:2:2:bin:/bin:/usr/sbin/nologin
whoopsie:x:112:117::/nonexistent:/bin/false

3. Display contents multiple Files

You can also display the contents of multiple files.

Here I have two files named months.txt and week.txt. Run the following command to display the contents of these files.

~$ cat months.txt week.txt 
January
February
March
April
May
June
July
August
September
October
November
December
Monday
Tuesday
Wedneswday
Thursday
Friday
Saturday
Sunday
Display contents of Multiple Files

4. Append (Add) data to a existing File

To append (add) data to an existing file, you must use the Append the output operator (>>) with the cat command.

~$ cat >> test.txt

After running the above command, it will allow you to type the text you want to append in the test.txt file.

After typing, press the CTRL + D (Hold the CTRL button and then press D) button on your keyword to save the file.

append data to a existing file

Note: Never use the Redirection of output (>) operator when appending data to an existing file as it will override the current data of that file.

5. Redirect contents of Files using different Operators

By default, cat command displays the contents of the file on Standard Output (stdout), but you can also redirect the output to a new/existing file.

Let’s take some examples:

Here I have two files named months.txt and week.txt with some content. With the help of these files, I will explain this concept to you.

Ex # 1: Copy the contents of months.txt to a new file named test.txt.

~$ cat months.txt > test.txt

Ex # 2: Copy the contents of week.txt to an existing file named test.txt.

~$ cat week.txt >> test.txt

6. Concatenate (Link) files using cat command

Using cat you can concatenate (Link) the contents of multiple files to a single file.

Let’s take an example:

Here I have two files named months.txt and week.txt and, I will concatenate the contents of these files to a new file named newfile.txt with the help of Redirection of output operator (>).

Refer the following command.

~$ cat months.txt week.txt > newfile.txt

You can also concatenate the content of multiple files to an existing file using Append the output operator (>>).

Example:

~$ cat months.txt week.txt >> newfile.txt

7. Usage of Wildcards

You can use Wildcards with the cat. Let’s take some examples.

Ex # 1: Display the contents of all available files in a directory.

~$ cat *

Ex # 2: List the contents those files whose extension is “.txt“.

~$ cat *.txt

Ex # 3: List the contents of those files that start with “te“.

~$ cat te*

8. Combine cat command with other commands

You can combine the cat with other commands to get your desired output.

Let’s take some examples:

Ex # 1: Combine cat with the head command.

Note : By default, head command prints the first ten lines of a file, but you can display as many lines as you want using its other features.

Task # Display first 3 lines of a file.

~$ cat test.txt | head -n3
January
February
March

Ex # 2: Combine cat with the tail command.

Note : By default, tail command prints the last ten lines of a file, but you can display as many lines as you want using its other features.

Task # Display last 3 lines of a file.

~$ cat test.txt | tail -n3

Ex # 3: Combine cat with more or less command.

Note: more and less commands are typically used to view a large file. It displays one screen at a time.

~$ cat /etc/passwd | more
~$ cat /etc/passwd | less

9. Number all output lines

To number all output lines pass the -n option to cat.

~$ cat -n months.txt 
     1	January
     2	
     3	February
     4	
     5	March
     6	April
     7	May
     8	June
     9	July
    10	August
    11	September
    12	October
    13	November
    14	December

But here is a problem. With the usage of -n option, it numbers both empty and non-empty output lines.

Numbered Empty Output Lines

To number only non-empty output lines pass the -b to cat command.

~$ cat -b months.txt 
     1	January

     2	February

     3	March
     4	April
     5	May
     6	June
     7	July
     8	August
     9	September
    10	October
    11	November
    12	December

10. Display $ at end of each line

To display the dollar sign ($) at the end of each line, pass the -E option to cat command.

~$ cat -E week.txt 
Monday$
Tuesday$
Wedneswday$
Thursday$
Friday$
Saturday$
Sunday$

You can also use the -e option to get the similar output.

~$ cat -e week.txt 
Monday$
Tuesday$
Wedneswday$
Thursday$
Friday$
Saturday$
Sunday$

11. Display TAB characters as ^I

You can display the Tab characters, which is also called as non-printing characters.

To do so, pass the -T option to cat command.

~$ cat -T week.txt 
Monday : First^IDay^Iof^Ithe^IWeek.
Tuesday : Second^IDay^Iof^Ithe^IWeek.
Wedneswday : Third^IDay^Iof^Ithe^IWeek
Thursday : Fourth^IDay^Iof^Ithe^IWeek.
Friday : Fifth^IDay^Iof^Ithe^IWeek.
Saturday : Sixth^IDay^Iof^Ithe^IWeek.
Sunday : Seventh^IDay^Iof^Ithe^IWeek.
Display Tab Characters

12. Suppress repeated empty output lines

To suppress repeated empty output lines pass the -s option to cat.

~$ cat -s months.txt

13. Display system Information’s using cat command

In Linux, typically all system information related files are stored in the /proc directory.

And most of them are simple text files that can appear using cat command.

So let’s check some system-related information using cat.

CPU Information:

~$ cat /proc/cpuinfo

Memory Information:

~$ cat /proc/meminfo

Partition Information:

~$ cat /proc/partitions

Swap Information:

~$ cat /proc/swaps

14. Print the files using cat command

To take print out of files using cat type the following command. For this, you have to use the Pipe (|) operator.

~$ cat output.txt | lpr

15. Help/Manual page access

Use the following commands to access the Manual Page/Help Page of cat command.

~$ cat --help
~$ man cat

16. Check the version of cat command

Check the cat command version using the following command.

~$ cat --version

Infographic

Refer to this Infographic for complete cat command options.

Cat Command Complete Checklist

You can visit at following websites to get more information on cat command.

Conclusion

I hope you have learned something from this article and you may have found that cat is a very important command in Linux.

I have tried my best to include all the features of cat command in this guide.

Now I’d like to hear your thoughts.

Was this guide useful to you?

Or maybe you have some queries.

Have I not included any command in this guide?

Leave a comment below.

If you like our content, please consider buying us a coffee.

Buy Me A Coffee

We are thankful for your never ending support.

Leave a Comment