cat Command in Linux

Back

The most basic command for reading files in Linux is cat command. The cat file-name command scrolls the text within the file-name file.
 It also works with multiple file-names ,it concatenates the file-name that you might list as one continuous output to your screen. 

You can redirect the output to the file-name of your choice, as described. 


The cat command is considered as one of the most frequently used commands on Linux or UNIX like operating systems.

It can be used for the following purposes under UNIX or Linux:

1) Display text files on screen.
2) Copy text files.
3) Combine text files.
4) Create new text files.

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

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

How Do I Create a File?

You can use cat command for file creation. To create a file called foo.txt, enter:
    $ cat > foo.txt

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