Linux cat Command Tutorial

Linux provides the cat command in order to print file content into the terminal or command line interface. Even though it may seem a simple command the cat command provides different features for different tasks In this tutorial we examine the Linux cat command to print files, display line numbers, create file etc.

Display File Content

The most popular and used feature of the Linux cat command is displaying or printing file contents. We can provide the file name to the echo command as a parameter.

$ cat /etc/passwd
Display File Content

Display Multiple Files Content

We can display multiple file content with a single cat command execution like below. We provide the file names by separating them with spaces. In the following example we print the files “test.py” and “notes.txt”. We can also add more files to display their contents.

$ cat test.py notes.txt

Create File

We can create a file using the cat command. We use the > redirection operator to redirect content interactively into the specified file. In the following example, we create the file named “db.txt”. We can use the CTRL+D in order to stop interactive input into the file db.txt .

$ cat >db.txt

Redirect File Content To Other Commands

Another popular use case for the cat command is the ability to redirect content to the other commands. The | (pipe) operator is used to redirecting file content with the cat command. In the following example, we redirect the log.txt content into the less command.

$ cat log.txt | less

Display Line Numbers

While displaying file contents we can print the line numbers using the -n option.

$ cat -n /etc/passwd

Display Tabs with ^

We can print tabs as ^ by using the -T option.

$ cat -T /etc/passwd

Display File End with $

The cat command generally displays files with multiple lines. In order to detect end of lines we can use the -e option to put $ every line end.

$ cat -e /etc/passwd
Display File End with $

Redirect File To Standard Output

The > operator can be used to redirect a file content into the standard output which can be another command or file.

$ cat db.txt > newdb.txt

Append To File

The Linux cat command can be used to append file content into the existing file without overwriting it. The >> operator can be used to append into the file.

$ cat db.txt >> newdb.txt

Leave a Comment