How To Use cat Command In Linux / UNIX

How do I use cat command in Linux? How do I use cat command in UNIX? How can I use cat command in UNIX or Linux shell scripts?

The cat command is considered as one of the most frequently used commands on Linux or UNIX like operating systems. This page shows how to use cat command with basic examples and usage for Unix/Linux system.

It can be used for the following purposes:

  • Display text files on screen.
  • Copy text files.
  • Combine text files.
  • Create new text files.

cat command Syntax

The syntax is as follows:

cat filename
cat options filename
cat file1 file2
cat file1 file2 > newcombinedfile

Displaying The Contents of Files

To read or read the contents of files, enter:
$ cat /etc/passwd
The above command will display the contents of a file named /etc/passwd. By default cat will send output to the monitor screen. But, you can redirect from the screen to another command or file using redirection operator as follows:
$ cat /etc/passwd > /tmp/test.txt
In the above example, the output from cat command is written to /tmp/test.txt file instead of being displayed on the monitor screen. You can view /tmp/test.txt using cat command itself:
$ cat /tmp/test.txt

Concatenate files

Concatenation means putting multiple file contents together. The original file or files are not modified or deleted. In this example, cat will concatenate copies of the contents of the three files /etc/hosts, /etc/resolv.conf, and /etc/fstab:
$ cat /etc/hosts /etc/resolv.conf /etc/fstab
You can redirect the output as follows using shell standard output redirection:
$ cat /etc/hosts /etc/resolv.conf /etc/fstab > /tmp/outputs.txt
$ cat /tmp/outputs.txt

You can also use a pipe to filter data. In this example send output of cat to the less command using a shell pipe as the file is too large for all of the text to fit on the screen at a time:
$ cat /etc/passwd | less

How Do I Create a File?

To create a file called foo.txt, enter:
$ cat > foo.txt
Sample outputs:

This is a test.

To save and exit press the CONTROL and d keys (CTRL+D). Please note that if a file named foo.txt already exists, it will be overwritten. You can append the output to the same file using >> operator:
$ cat >> bar.txt
The existing bar.txt file is preserved, and any new text is added to the end of the existing file called bar.txt. To save and exit press the CONTROL and d keys (CTRL+D).

How Do I Copy File?

The cat command can also be used to create a new file and transfer to it the data from an existing file. To make copy of
$ cat oldfile.txt > newfile.txt
To output file1’s contents, then standard input, then file2’s contents, enter:
$ cat file1 - file2
A hyphen indicates that input is taken from the keyboard. In this example, to create a new file file2 that consists of text typed in from the keyboard followed by the contents of file1, enter:
$ cat - file1 > file2

cat command options

To number non-blank output lines, enter (only works with GNU cat command version):
$ cat -b /etc/passwd
Sample outputs:

     1	root:x:0:0:root:/root:/bin/bash
     2	daemon:x:1:1:daemon:/usr/sbin:/bin/sh
     3	bin:x:2:2:bin:/bin:/bin/sh
     4	sys:x:3:3:sys:/dev:/bin/sh
     5	sync:x:4:65534:sync:/bin:/bin/sync
     6	games:x:5:60:games:/usr/games:/bin/sh
     7	man:x:6:12:man:/var/cache/man:/bin/sh
     8	lp:x:7:7:lp:/var/spool/lpd:/bin/sh
     9	mail:x:8:8:mail:/var/mail:/bin/sh
    10	news:x:9:9:news:/var/spool/news:/bin/sh

To number all output lines, enter (GNU cat version only):
$ cat -n /etc/passwd
To squeeze multiple adjacent blank lines, enter (GNU cat version only):
$ cat -s /etc/passwd
To display all nonprinting characters as if they were visible, except for tabs and the end of line character, enter (GNU cat version only):
$ cat -v filename

cat Command Abuse

The main purpose of cat is to catenate files. If it’s only one file, concatenating it with nothing at all is a waste of time, and costs you a process. For example,
$ cat /proc/cpuinfo | grep model
Can be used as follows:
$ grep model /proc/cpuinfo
Another example,
cat filename | sed -e 'commands' -e 'commands2'
Can be used as follows which is cheaper:
sed sed -e 'commands' -e 'commands2' filename
See useless use of cat command for more information.

🐧 Get the latest tutorials on Linux, Open Source & DevOps via RSS feed or Weekly email newsletter.

🐧 25 comments so far... add one

CategoryList of Unix and Linux commands
Disk space analyzersncdu pydf
File Managementcat
FirewallAlpine Awall CentOS 8 OpenSUSE RHEL 8 Ubuntu 16.04 Ubuntu 18.04 Ubuntu 20.04
Network UtilitiesNetHogs dig host ip nmap
OpenVPNCentOS 7 CentOS 8 Debian 10 Debian 8/9 Ubuntu 18.04 Ubuntu 20.04
Package Managerapk apt
Processes Managementbg chroot cron disown fg jobs killall kill pidof pstree pwdx time
Searchinggrep whereis which
User Informationgroups id lastcomm last lid/libuser-lid logname members users whoami who w
WireGuard VPNAlpine CentOS 8 Debian 10 Firewall Ubuntu 20.04
25 comments… add one
  • jason Sep 20, 2011 @ 11:51

    Hi I used the following command
    $cat file
    $cat file2
    $cat file1 – file2
    test2 – It got displayed twice and now its like never ending and I have to press Ctrl+c to close it and the out put of file3 never got displayed….Can you help me out with this command ?

    • jason Sep 20, 2011 @ 12:02

      I did Ctrl+D and got
      but why i am getting two test2 in the output ?

    • inba Feb 14, 2014 @ 11:13

      1 step: to creat new file (vim file1.txt) and save cmand(:wq)
      $ cat > foo.txt
      2 step: to creat anoter new file (vim file2.txt) and then open new file if any words or commands type and to save that file(:wq)
      $ cat >> bar.txt
      3 step: $ cat file1.txt > file2.txt
      $ cat oldfile.txt > newfile.txt
      4 step: $ cat file1.txt – file2.txt

  • onne Apr 20, 2012 @ 13:25

    great article !

  • mann saini Jun 25, 2012 @ 15:05

    it should be like this

    $cat > test1
    Ctrl +D

    $cat > Test2

    $Cat Test1 Test2 > Test3

    now Test3 must display like this

    $Cat Test3



    • jewel.saha Jul 10, 2013 @ 3:17

      Exactly !!!!

  • shahin Jan 20, 2013 @ 17:28

    how i can cat an error help plzz!!!

  • sunil Feb 19, 2013 @ 8:01

    well i check it work or not

  • raza May 22, 2013 @ 6:50

    this is really awesome article .

  • Britt Jul 1, 2013 @ 3:22

    What does this command do? ls | grep cat*.* > listdir.txt

  • durga Sep 2, 2013 @ 5:48

    what is cat X in unix

  • James Oct 7, 2013 @ 21:48

    I’m in a Linux intro class, having trouble with syntax and getting help. I entered:
    cat > numbers
    ctrl + d
    This puts the output into a file. Now I have to find a command that consolidates the last 2 “42’s” into a single piece of input. It’s nothing but a training exercise, but it’s bothering me that I can’t find it or get help from the instructor.
    Any help is appreciated.

  • prabhat Jan 17, 2014 @ 13:35

    james: u can use uniq command.
    just write

  • prabhat Jan 17, 2014 @ 13:36

    uniq filename

  • lurvas777 Apr 4, 2014 @ 20:41

    Tanks a ton!! This article cleared things up and have helped me a lot!
    What’s great is the abuse section, from which I learned even more! (about being cautious with big files and actually considering what the command does)

    Keep up the great work :D

  • Thunderbolt May 19, 2014 @ 0:03

    Nice article…..I am new to linux…and i need these things..

  • Tiger Jun 4, 2014 @ 13:06

    plzzz anyone tall me that how to work cat command….. not use of tell command i want to know about processing of cat command

  • nani Oct 19, 2014 @ 18:16

    Hi every one , i am new in using linux commands please help me out which command i can use
    Either the foo process, the process was launched from a console. I want to get the hands on the console without killing foo.
    What are the actions i can take:

    [A] c then press CTRL & foo
    [B] then press CTRL z foo &
    [C] do CTRL z then bg foo
    [D] then press CTRL c foo

  • reza Jul 24, 2015 @ 16:17


    thanks that work currectly.

  • dilkhush Oct 5, 2015 @ 6:33

    Haee,..,frndz can u help me to use command who use in linux.

  • twinkle Oct 11, 2015 @ 18:29

    what does ‘>’mean in cat > filename command

  • hi Feb 22, 2016 @ 16:54

    what does cat <> ……. E0F mean????

  • Rhys Darby Jul 18, 2016 @ 12:41

    Hello. I would like to know how to show the content of the file that contains the Bash history and I would also like you to explain what the content means.

    Thank you

  • fcradvocate Aug 28, 2016 @ 12:08

    Thank you for the site and the great info you provide. Don’t take this wrong but, I gotta tell ya… the light gray text on a white background is tough on this old mans eyes.

  • Mani Feb 1, 2017 @ 20:14

    When i concatenate 2 files, first line of 2nd file is appended with last line of first file. How to make it to display as a separate line?

    1234|GM America|400

    ABC|GM America|400

    When i run,
    cat Sample1.dat Sample2.dat > Sample3.dat

    Output is looking like below,
    1234|GM America|400
    3456|Coke|600ABC|GM America|400

Leave a Reply

Your email address will not be published.

Use HTML <pre>...</pre> for code samples. Still have questions? Post it on our forum