How to Use the Cat Command in Linux [22 Useful Examples]

The cat (short for “concatenate“) command is one of the most frequently used commands in Linux that comes pre-installed in most Linux distribution systems and is primarily used to display the content of existing files.

Moreover, the cat command can be utilized by the user to concatenate multiple files, create new files, append content to existing files, view the content of a file, and redirect output in the terminal or files.

The cat command can also be used to format the output of the file with the help of different options, such as adding numbers before each line of the file’s content.

Additionally, it can execute in combination with other commands to perform various tasks including providing page navigation and conversion of file format to binary or hexadecimal.

In this article, we are going to find out the handy use of cat commands with their examples in Linux.

Cat Command Syntax

The cat command can accept multiple options and file name arguments as shown:

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

Let’s understand the above syntax:

  • [OPTION] – Users can provide multiple options to alter the behavior of the command. The options start with a hyphen ("-"), such as "-E" is used to display line ends and "-n" to display numbers before lines.
  • [FILE] – The file argument specifies the file which will be manipulated by the command. However, users can provide names of multiple files separated by space.
Note: To read in detail about all the available options of the cat command, execute the "cat --help" command in your Linux terminal:
$ cat --help
Cat Command Help
Cat Command Help

Let’s explore different examples to harness the power of cat command.

1. Display Contents of a File in Linux

The basic functionality of the cat command is to display the content of an existing file in Linux. For that purpose, provide the name of the file with no option as shown.

$ cat Documents/tecmint1.txt

Here in the command, the content of the file “tecmint1.txt” which is located in the “Documents” directory will display.

View File Contents in Linux
View File Contents in Linux

2. Display Contents of Multiple Files in Linux

The cat command can also be utilized to show the contents of more than one file by providing the file names separated by space as shown:

$ cat tecmint1.txt tecmint2.txt 
View Multiple Files Contents
View Multiple Files Contents

In the above output, we can see the contents of both files in the terminal. The first two lines are of file “tecmint1.txt”, whereas the last line of the output is the content of the “tecmint2.txt” file.

3. Create a File with Cat Command

The user can create a new file and save content in it with the ">" symbol (known as the “output redirection operator”) will redirect the output of the command to the file specified by the filename “Tecmint_tutorial.txt” as shown.

$ cat > Tecmint_tutorial.txt
Note: If you want to create a new file, be careful that a file with the same name does not exist already. Otherwise, the command will overwrite the content of the existing file.

After executing the command, an indicator will blink in the new line. Write the content for the file and press the “CTRL + D” keys to save and exit the file:

Create New File and Add Content
Create New File and Add Content

You can verify the file’s creation by using the ls command and use the cat command to view the content of the newly created file:

$ ls
$ cat Tecmint_tutorial.txt
View File Contents
View File Contents

4. Append Text to a File in Linux

One of the benefits of the cat command is that it can append the content to an existing file using the ">>" symbol (known as “append redirection operator”) will append/combine the additional content to an existing file “Tecmint_tutorial.txt“.

$ cat >> Tecmint_tutorial.txt

Type or paste the content that you want to append to this file and press the "CTRL + D" keys:

Append Text to a File
Append Text to a File

Now let’s verify if the content has been appended to the existing contents stored in the file:

$ cat Tecmint_tutorial.txt
Check File Contents
Check File Contents

5. Copy File Content to Another File in Linux

Sometimes, the user wants to create a copy of the contents stored in a file into a new file for different purposes, such as backup. Here, ">" operator will read the content of the “Tecmint_tutorial.txt” file sequentially and will place it into a new file named “New_file.txt“.

$ cat Tecmint_tutorial.txt > New_file.txt

The next step is that you verify if the new file has been created successfully by running:

$ ls
$ cat New_file.txt
Copy Contents From One File to Another
Copy Contents From One File to Another

6. Append Contents of Multiple Files Into One File on Linux

As mentioned earlier, the cat command can be utilized for concatenation purposes. Let’s run the command to concatenate/merge contents of the “tecmint1.txt” and “tecmint2.txt” files and store the result in a new file named “cat_tecmint.txt”:

$ cat tecmint1.txt tecmint2.txt > cat_tecmint.txt

The above command will read the content of the “tecmint1.txt” and “tecmint2.txt” files and will write them in a new file “cat_tecmint.txt”.

Moving forward, we need to verify whether the new file stores the concatenated content of both files or not:

$ ls 
$ cat cat_tecmint.txt
Concatenate Multiple Files in Linux
Concatenate Multiple Files in Linux

7. View File Content with Line Endings

The user can also use the "-E" option to view the EOL (End of Line) character in the contents of the file. The EOL characters are known as non-printing characters and they are represented by the dollar ("$") symbol.

$ cat -E cat_tecmint.txt 

The expected output will show the "$" symbol at the end of each line of the content.

View File Content with End of Line
View File Content with End of Line

8. List Contents of All Specified File Types

The cat command can use the "*" wildcard character to list the content of all files available in the current directory. Additionally, you can also specify any particular file type such as ".txt" followed by a wildcard character to show the content of all “txt” files available in the directory.

$ cat *.txt
List Contents of Files by File Types
List Contents of Files by File Types

The output portrays the content of all “txt” files one after one.

9. Display Line Numbers in File

If you want to show line numbers prior to each line of the file’s content, use the "-n" option to display the line number in the output without any changes to the original content of the file.

$ cat -n Fruits.txt
Show Line Numbers in File
Show Line Numbers in File

The expected output will show line numbers before each line.

10. Print Line Numbers of Multiple Files

The cat command with the "-n" option can also work on multiple files by concatenating the contents of multiple files and adding numbers prior to each line of combined output.

$ cat -n Fruits.txt veg.txt

Here in the command, the content “Fruits.txt” and “veg.txt” files will combine, and then the "-n" option will add line numbers at the beginning of each line of the output.

Print Line Numbers of Multiple Files
Print Line Numbers of Multiple Files

11. Show File Contents with Tab Characters

The "-T" option can display the tab spaces characters "^I" in the output which are known as non-printing characters.

$ cat -T tabfile
Show File Contents with Tab Characters
Show File Contents with Tab Characters

The output contains the tab characters in place of tab spaces.

12. View File Contents with More Command

Some files contain a lot of content that does not entirely fit in the output screen of the terminal. If we use the simple cat command to display the content of such files, the output does not indicate that more content is available and the user needs to scroll down to see it.

$ cat tutorial.txt
View File Contents
View File Contents

Here in the output, we can see only some of the actual content.

To resolve this issue, you can use the pipe "|" symbol that helps in using the output of one command as the input to another command, in this case, it is the “more” command that offers page navigation at the end of the file.

$ cat tutorial.txt | more

13. View File Contents with Less Command

You can also use less commands to view the contents of a file in a scrollable and searchable manner using the keys.

$ cat tutorial.txt | less

14. Suppress Repeated Empty Lines in Output

In some cases, the user leaves repeated empty lines by mistake instead of a single empty line. However, the cat command can be utilized to suppress repeated empty lines from the content of a file with the help of the "-s" option.

$ cat -s tutorial.txt
Suppress Repeated Empty Lines in File Output
Suppress Repeated Empty Lines in File Output

The output has only single empty lines, all repeated lines are suppressed successfully.

15. Append File Content to End of Another File

The cat command can append the content of a file at the end of another file by using the ">>" symbol (known as “append redirection operator”).

$ cat Fruits.txt >> veg.txt 
$ cat veg.txt
Append One File Content to Another
Append One File Content to Another

The output shows that the contents of both files are appended in the “veg.txt” file.

16. Display File Contents in Reversed Order

To display the file’s content in reverse order, use the tac command, which is also known as ‘cat’ backward that displays the last line first, then the second last, and so on.

$ tac Weekdays.txt
View File Contents in Reverse Order
View File Contents in Reverse Order

The output displays the content of the “Weekdays.txt” in reverse order.

17. Show File Content in Binary Format

The cat command can be used in combination with the “xxd” utility along with the "-b" option to convert the contents of the file into the binary format.

$ cat Weekdays.txt | xxd -b

Let’s break down the above command:

  • | – The pipe symbol (|) will give the output of the cat command to the command (xxd -b).
  • xxd – It is a utility that will convert content into a hexadecimal representation.
  • -b – This option is used with xxd to specify the binary output format instead of the default hexadecimal format.
View File Content in Binary Format
View File Content in Binary Format

The output portrays the file’s content in binary format and the original format side by side.

18. Show File Content in Hexadecimal Format

To convert the content of a file into the hexadecimal format, the user can utilize the “hexdump” utility as shown below:

$ cat Weekdays.txt | hexdump -C

Here in the command, the pipe symbol is joining both commands whereas the “hexdump” command will convert the content into hexadecimal format. Additionally, the "-C" option will show the ASCII representation alongside the hexadecimal values.

View File Content in Hexadecimal Format
View File Content in Hexadecimal Format

The output shows the converted content of the file in hexadecimal format successfully.

19. Display Specific Lines of File in Linux

The cat command can be combined with the sed command to display a specific range of lines from a file that matches the defined pattern from the file.

$ cat Weekdays.txt | sed -n '3,6p'

Here in the above command, the sed command will get the output of the cat command as input with the help of the pipe symbol. Then the sed command with the option "-n" and pattern “3,6p” will print lines 3 to 6 from that input.

View Specific Lines of File Content
View Specific Lines of File Content

20. Sort the Contents of File Alphabetically

The user can utilize the cat command with the sort command to alphabetically sort the lines of content as shown.

$ cat -v veg.txt | sort
View File Content Alphabetically
View File Content Alphabetically

The output displays the alphabetically sorted content of the file.

21. Display End of File Marker in File

The cat command can be utilized with the “here document” which aids the user to input the content into the file and set the page end marker.

The “here document” is denoted by the "<<" symbol followed by a delimiter "EOF", which allows the user to input multiple lines of text directly from the terminal and save them into a file.

The input process is terminated by entering the specified delimiter on a new line.

$ cat > month.txt << EOF
View End of File Marker in File
View End of File Marker in File

The output saved the input in the file “month.txt” and terminated the input process when “EOF” was entered.

22. View CPU Info in Linux

The cat command can also display the content of a virtual file named “cpuinfo”, which contains information about the CPU processor, model name, cache size, number of cores, and other details of the CPU.

$ cat /proc/cpuinfo
View Linux CPU Info
View Linux CPU Info

That is it for this blog, I hope you understood the usage of the cat command in Linux. You may refer man page of the cat command if you want to know more options.

$ man cat
Conclusion

This article displayed 20 examples of the cat command in Linux to concatenate, display, and create files. Additionally, the cat command can be utilized with other Linux commands to perform more advanced operations like sorting content or converting the content into other file formats.

Hey TecMint readers,

Exciting news! Every month, our top blog commenters will have the chance to win fantastic rewards, like free Linux eBooks such as RHCE, RHCSA, LFCS, Learn Linux, and Awk, each worth $20!

Learn more about the contest and stand a chance to win by sharing your thoughts below!

Ravi Saive
I am an experienced GNU/Linux expert and a full-stack software developer with over a decade in the field of Linux and Open Source technologies

Each tutorial at TecMint is created by a team of experienced Linux system administrators so that it meets our high-quality standards.

Join the TecMint Weekly Newsletter (More Than 156,129 Linux Enthusiasts Have Subscribed)
Was this article helpful? Please add a comment or buy me a coffee to show your appreciation.

47 Comments

Leave a Reply
  1. Hi, sir how to create multiple files in cat command (remaining examples very nice and thanks and you are doing very well lot of thanks…

    Reply
  2. [edyst]>>> Used the cat command successfully!
    [edyst]>>> Used the tac command successfully!
    [edyst]>>> You used the head command correctly!
    [edyst]>>> You used the tail command correctly
    [edyst]>>> You copied the first 6 lines of avengers.txt successfully!
    [edyst]>>> You copied the last 3 lines of avengers.txt successfully!
    [edyst]>>> You reversed the contents of avengers.txt correctly
    [edyst]>>> Task executed successfully!

    whats the input ?

    Reply
  3. We can use parameters more and less. They’re not parameters, you’re piping the “cat” output to those commands. Big difference.

    Reply
    • I think it will give an error :/ ==> because of “ls -l” there is no such file or directory

      If not please aware us too.

      Reply
  4. How can I append an md-file to another md-file in a sub-folder? I tried “cat %U >> subfolder-name/%u“, but I get this error message “cannot create subfolder-name//path/to/md-file/md-file.md: Directory nonexistent“.

    Can you help me?

    Reply
  5. Hi @Ravi,

    This is a really good stuff for beginners, like me by the way. About cat command, I have a quite different question: When I use that within a bash shell script that captures and display some output, how do I close the command processing? I mean, after running the script, I needed to press Enter or type Ctrl+C / Ctrl+D, and I would like to have the script terminated after the run.

    the lines I’ve entered in my script are:

    echo `cat | ls -ltr > temp3_mask_000`
    echo `cat | du -a >> temp3_mask_000`
    

    Thank you anyway for this teaching!

    Reply
    • @Lenardo,

      At the end of your shell script, set exit 0 to close the shell script as shown.

      echo `cat | ls -ltr > temp3_mask_000`
      echo `cat | du -a >> temp3_mask_000`
      # Terminate our shell script with success message
      exit 0
      
      Reply
  6. #ZombieRessurection – please do not spread bad practices

    cat | more file” is not smart as “cat file file | sort > newfile” isn’t
    use
    less file
    or
    sort file file > newfile

    for selecting rows comfortable use sed instead:

    sed -n '4,6p' file
    

    will print lines 4-6 from

    Reply
    • @Kent,

      Oops what a big mistake and was not realized or pointed by anyone so far, thanks a ton Kent for notifying us, we’ve replaced the word contains with content in the writeup..really thankful to you..

      Reply
    • @Dan,

      You mean on Windows 10 with Bash on it? If you have Bash installed on your Windows, all cat command examples shown here will work without any errors..

      Reply
  7. Hi Ravi thanks.

    How can I select lines (i.e. rows) 10 to line 15 from a given file, let, ABC.txt, using cat command:

    Suppose I use the following command for getting the first 20 lines and 10 columns of the file ABC.txt which contains a table

    $ cat ABC.txt | head -20| cut -f1-10 
    

    Looking forward.

    Reply
  8. can somebody please explain this code below – the line that starts with cat. it seems like the code does not work since our report still prints as portrait, which may be the default printer setup for orientation. we even manually triggered the code but it still did not work. your help is appreciated. thanks so much…

    ISP_PRNTR=mcsprt ; export ISP_PRNTR

    #Extract the last 3 characters (file extension)
    ext=`echo $1|awk -F . ‘{print $NF}’`
    #Extract the file name without extension
    file=`basename $1`
    file=`echo $file|sed ‘s/\.[^.]*$//’`

    cat ${1} | /usr/bin/acroread -toPostScript | lp -d $ISP_PRNTR -o landscape

    Reply
    • From the code:

      This part of the code has not been used in the last command that prints the file, though it only deals with the file name which should not affect the layout during printing.

      #Extract the last 3 characters (file extension)
      ext=`echo $1|awk -F . ‘{print $NF}’`
      #Extract the file name without extension
      file=`basename $1`
      file=`echo $file|sed ‘s/\.[^.]*$//’`
      

      The variable file has not been used in command;

      cat ${1} | /usr/bin/acroread -toPostScript | lp -d $ISP_PRNTR -o landscape
      

      Instead the original filename, $1 has been used.

      It could be a problems of printer specific options that are required.

      Reply
  9. while viewing large file using cat command. how to move immediately to end of the file and how to exit from the file. I used Cntrl+d. But couldn’t exit. and If I need to edit the file, how can I edit using command [vi].

    Reply
    • They are two very different methods of achieving the same result.

      ‘cat file’ takes file as an argument, opening the file and displaying its contents

      ‘cat < file' opens file, and redirects its contents to stdin, so the cat command will take the contents of the file as if they were being typed in by a keyboard.

      To better understand what's going on, try this:

      in terminal 1:

      $ mkfifo test
      $ cat test

      In terminal 2, start typing. Every time you hit enter, you will see the text you just typed appear in terminal 1. Hit Ctrl+D and it will kill both the running cats.

      Reply
      • I don’t know why it deleted half my text, but let’s try this again.

        Terminal 1 should have a cat command, and terminal 2 should also have a cat command. If you don’t see two cat commands, then a bot is filtering my comment.

        Terminal 1:

        $ mkfifo test
        $ cat test

        Now you can start typing into terminal 2.

        Reply
        • Ah, I figured out what’s going on: It thinks I’m trying to type HTML.

          One more time:

          T1:

          $ mkfifo test
          $ cat < test

          T2:

          $cat > test

          Reply
          • Moderators: If you see this chain of self-replies, please fix it and edit the original reply to convey what I’m trying to convey.

            If you didn’t already figure out what’s going on, I used &lt; and &gt; to generate < and >, otherwise, “this <te>xt” becomes “this xt” because it is interpreted as a tag.

            Also, the ability to edit comments would be much welcomed. I hate to mess up a discussion thread with a million self-replies that try to figure out why X didn’t work.

            Thanks

    • #11 is total B.S.

      ‘cat < file' does not take 'file' as an argument.

      What 'cat < file' does, is use the contents (not "contains") of file as the input for cat, taking the place of the keyboard. It is functionally equivalent to 'cat file', and is thus completely redundant.

      Reply
  10. Q. Hi All, Can you show me what is the command or terminal to use with this question “Search lines which contains alpha-numeric words( combination of alphabets and number) and copy those lines is sorted order to /root/lines (output should not contain any blank lines)”. for example I have a file called Searchline.txt. thank you.

    Reply
  11. Why “cat test; cat test1; cat test2” ?

    Why not “cat test test1 test2” ?

    Why “cat song.txt | more” instead of “more song.txt”?

    Reply

Got Something to Say? Join the Discussion...

Thank you for taking the time to share your thoughts with us. We appreciate your decision to leave a comment and value your contribution to the discussion. It's important to note that we moderate all comments in accordance with our comment policy to ensure a respectful and constructive conversation.

Rest assured that your email address will remain private and will not be published or shared with anyone. We prioritize the privacy and security of our users.