Linux cat command with Examples

In this tutorial, we are going to explain Linux cat command with Examples. cat stands for catenate. It helps us to create, view, concatenate files.

It is mostly used for:

  • Display text files
  • Copy text files into a new document
  • Append the contents of a text file to the end of another text file, combining them<
  • /ul>

    What is Linux cat command?
    cat – concatenate files and print on the standard output

    Linux cat command Syntax:

    cat [OPTION]... [FILE]... or cat > filename or cat file3 | command cat file4 | grep something

    Options are explained at the end of this article.

    Linux cat command Examples:

    1. Display contents of file using cat command:

    For example, the following command will display the contents of the dir.sh file in the terminal:

    cat dir.sh

    Linux cat command with Examples

    2. Display contents of multiple files using cat command:

    Following command will display the contents of the test1.txt and check.txt files in the terminal:

    cat test1.txt check.txt

    3. Display line numbers in file using cat command on terminal:

    To get line numbers on terminal for contents of file use -n option with cat command as shown below.

    cat -n filefind.sh

    4. Create file using Linux cat command

    cat login.sh

    Awaits input from user, type desired text and press CTRL+D (hold down Ctrl Key and type ā€˜dā€˜) to exit. The text will be written in test2 file.

    5. Use Cat Command with More & Less Options

    If file is having large number of content that won’t fit in output terminal and screen scrolls up very fast, we can use parameters more and less with cat command as shown below.

    cat /etc/passwd | less cat /etc/passwd | more

    6. Appending Standard Output with Redirection Operator

    We can append the contents of one file to another by using >> operator with cat command.

    cat check.txt >> test1.txt

    Linux cat command with Examples

    7. Redirect one file data to another

    cat sends its output to stdout (standard output), which is usually the terminal screen. However, we can redirect this output to a file using the shell redirection symbol “>”.

    root@mongodb1:/home/r2schools/scripts# cat > new.txt root@mongodb1:/home/r2schools/scripts# cat new.txt root@mongodb1:/home/r2schools/scripts# cat check.txt > new.txt root@mongodb1:/home/r2schools/scripts# cat new.txt Welcome to r2schools Linux tutorials website

    Here first line create empty file.
    2nd command displays the content of file new.txt. No ouput because its empty.
    3rd command copies the contents of file check.txt to new.tx file.
    4th command displays the content of file new.txt.

    8. To view all files.

    cat *

    Options are:

    -A, –show-all: equivalent to -vET

    -b, –number-nonblank: number nonempty output lines, overrides -n

    -e: equivalent to -vE

    -E, –show-ends:: display $ at end of each line

    -n, –number: number all output lines

    -s, –squeeze-blank: suppress repeated empty output lines

    -t : equivalent to -vT

    -T, –show-tabs: display TAB characters as ^I

    -u (ignored)

    -v, –show-nonprinting:: use ^ and M- notation, except for LFD and TAB