16 Simple cat Command Examples For Linux

The ‘cat’ command is used to read and concatenate files, printing them out to standard output.

Cat comes from the GNU Coreutils package and should be available by default in Unix/Linux based operating systems.

By the end of this guide you should know how to use cat to read and view files in a number of different ways in Linux.

How To Use cat – Command Examples

  • 1. Read A File

    In its most simplest form, the cat command is run against a specific file which will output the content of the file to the screen. Here we read the content of the test.txt file which I have created for these examples.

    [[email protected] ~]# cat test.txt
    This is
    a text  file
    
    
    
    It      contains        text
    
            probably
    
  • 2. Read All Files

    We can also use wildcards to view all files in the directory.

    [[email protected] ~]# cat *
    

    We can modify this to only list the contents of say files with the .txt extension.

    [roo[email protected] ~]# cat *.txt
    
  • 3. Number Lines

    We can print out the number of each line with the -n option.

    [[email protected] ~]# cat -n test.txt
         1  This is
         2  a text  file
         3
         4
         5
         6  It      contains        text
         7
         8          probably
    
  • 4. Number Nonempty Lines

    Alternatively, we can use the -b option which overrides -n and will only number non blank lines, so lines with no content will not be numbered.

    [[email protected] ~]# cat -b test.txt
         1  This is
         2  a text  file
    
    
    
         3  It      contains        text
    
         4          probably
    
  • 5. Show Tab Character

    We can display tab characters as ‘^I’ with the -T option.

    [[email protected] ~]# cat -T test.txt
    This is
    a text^Ifile
    
    
    
    It^Icontains^Itext
    
    ^Iprobably
    
  • 6. Show End Of Line

    We can show where the line ends with -E which adds a ‘$’ to the end of each line.

    [[email protected] ~]# cat -E test.txt
    This is$
    a text  file$
    $
    $
    $
    It      contains        text$
    $
            probably$
    
  • 7. Show All

    We can also show all with -A which is similar to using both -E and -T together to display both line ends and tabs.

    [[email protected] ~]# cat -A test.txt
    This is$
    a text^Ifile$
    $
    $
    $
    It^Icontains^Itext$
    $
    ^Iprobably$
    
  • 8. Hide Repeated Blank Lines

    We can remove multiple blank lines with the -s option which will squeeze blank lines. In our example we have one section of the file that has 3 blank lines in a row, after using -s this is changed to just a single blank line.

    [[email protected] ~]# cat -s test.txt
    This is
    a text  file
    
    It      contains        text
    
            probably
    
  • 9. Read Multiple Files

    We can specify many more files after the first one with cat, it will simply read them all, concatenating the output together.

    [[email protected] ~]# cat test.txt test2.txt
    This is
    a text  file
    
    
    
    It      contains        text
    
            probably
    This is test2.txt
    

    Here we cat both test.txt and test2.txt and see the output displayed in this order.

  • 10. Concatenate Files Together

    Similar to the above example, we can concatenate multiple files together by appending one file to another. Note that this must be done with ‘>>’, if we use a single ‘>’ the file we are writing to will be overwritten rather than appended to.

    [[email protected] ~]# cat test2.txt >> test.txt
    [[email protected] ~]# cat test.txt
    This is
    a text  file
    
    
    
    It      contains        text
    
            probably
    This is test2.txt
    
  • 11. Concatenate Files Into New File

    We can also combine two files together to form a new file altogether.

    [[email protected] ~]# cat test.txt test2.txt > test-combination.txt
    [[email protected] ~]# cat test-combination.txt
    This is
    a text  file
    
    
    
    It      contains        text
    
            probably
    This is test2.txt
    This is test2.txt
    
  • 12. Reverse Cat Output

    By default the cat command reads the specified file from start to finish, there’s no built in way to reverse the output. There is however the ‘tac’ command (cat backwards) which works similarly to cat, except the lines in a file are printed out in reverse order.

    [[email protected] ~]# tac test.txt
    This is test2.txt
            probably
    
    It      contains        text
    
    
    
    a text  file
    This is
    
  • 13. Read Standard Input

    Instead of reading a file we can read from standard input, that is the command line.

    [[email protected] ~]# cat
    hello world!
    hello world!
    

    In this example after entering cat, followed by the text ‘hello world!’, the text was returned back from cat which is why it appears twice.

  • 14. Create A New File

    We can also create new files with the cat command by redirecting the output to a file using ‘>’ followed by the new file name. In this example, we run cat to read from standard input and send all output to the text3.txt file. Once we have entered our text, press ‘Ctrl+D’ to exit and we will then be able to cat the new file and confirm it contains what we have added.

    [[email protected] ~]# cat > test3.txt
    This is the test3 file from standard input!
    [[email protected] ~]# cat test3.txt
    This is the test3 file from standard input!
    
  • 15. Print Cat Version

    We can view the version information for the cat command with the --version option.

    [[email protected] ~]# cat --version
    cat (GNU coreutils) 8.22
    Copyright (C) 2013 Free Software Foundation, Inc.
    License GPLv3+: GNU GPL version 3 or later .
    This is free software: you are free to change and redistribute it.
    There is NO WARRANTY, to the extent permitted by law.
    
    Written by Torbjörn Granlund and Richard M. Stallman.
    
  • 16. Print Help Documentation

    If you have any further queries about the cat command you can always refer to the help page with the --help option. Please note that the full output of this command has been removed for brevity.

    [[email protected] ~]# cat --help
    Usage: cat [OPTION]... [FILE]...
    

Summary

As we have seen the cat command is fairly basic and easy to use, however it can be combined with other options to make it quite a useful tool to know about.

Leave a Comment

NOTE - You can use these HTML tags and attributes:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>