This comprehensive Linux guide expects that you run the following commands as root user but if you decide to run the commands as a different user then ensure that the user has sudo access and that you precede each of the privileged commands with sudo

cat (short for Concatenation) is one of the most frequently used commands in linux. It is used to perform different operations related to text files i.e. displaying, creating, copying and redirecting output to the terminal. In this tutorial, we will describe the different ways to use the cat command.

In this tutorial, we are using commands with examples. So, replace the “filename” with name of the file. 

General Syntax

The general syntax of cat command is given below:

cat [options] [filenames] [-] [filenames]

Displaying a File

cat command is mostly used to display the contents of files.

Displaying a Single File

If you want to display the contents of a single file, you can follow the command given below:

root@codesposts:~$ cat filename

Displaying Multiple Files

You can also use this command to display the contents of multiple files, for this, run the command like below:

root@codesposts:~$ cat filename1 filename2 filename3

Display Contents Preceding With Line Numbers

If you want to view the contents of the file with displaying the line numbers before each line, you can run the command like below:

root@codesposts:~$ cat -n filename

Displaying Large Files

If the file is too large for all of the text to fit on the monitor screen simultaneously, as is frequently the case, the text will scroll down the screen at high speed and be very difficult to read. This problem is easily solved by piping the output to a filter:

root@codesposts:~$ cat filename | more
OR
root@codesposts:~$ cat filename | less

These filters will display the file in parts, a screenful at a time. You can move forward by pressing space or you can move backwards by pressing b. To exit, you can press q.

Displaying File With Highlighting End Of Each Line

You can also highlight the end of each line using the cat command with option -e with it.

root@codesposts:~$ cat -e filename

Highlighting TAB Spaces

You can use the option -T with the cat command. The TAB spaces will be highlighted by ‘^I‘ character

root@codesposts:~$ cat -T filename

Suppressing Repeated Empty Lines In Output

You can also suppress the repeated empty line in the output of a file by using the option -s with the cat command.

root@codesposts:~$ cat -s filename

Combining Options With cat Command

You can combine different options with the cat command like -seT. Or you can just use -A with the cat command.

root@codesposts:~$ cat -A filename

Creating a File

You can create new files using the cat command. For this, run the command like this:

root@codesposts:~$ cat > filename

After running this command, the terminal will wait for you to enter the content of the file. You can exit by pressing ctrl+D keys.

Caution:

If the file with the name “filename” already exits, it will be overwritten and the old file will be lost. So, it’s better to use append operator while creating a file.

root@codesposts:~$ cat >> filename

Copying Files

You can copy the contents of a file into another file using cat command.

Replacing The Contents Of The Target File

If you use the cat command with the option > between two files, the contents of the source file will be copied to the target file and the existing contents of target file will be lost.

root@codesposts":~$ cat sourceFile > targetFile

Appending Contents At The End Of Target File

If you use >> option with cat command, the contents of the source file will be added at the end of the target file, without losing the contents of the target file.

root@codesposts:~$ cat sourceFile >> targetFile

Copying Multiple Files To A Single File

You can copy the contents of Multiple Files to a single file by running the command like below:

root@codesposts":~$ cat sourceFile1 sourceFile2 sourceFile3 > targetFile


Copying Multiple Files To A Single File In A Sorted Order

You can Copy the contents of multiple files into a single file while sorting them.

root@codesposts":~$ cat sourceFile1 sourceFile2 sourceFile3 | sort > targetFile

Using File As Input For A Command

You can use cat command to redirect the Standard Input by using less than (<) symbol with the cat command.

root@codesposts:~$ cat < filename