Signup/Sign In
PUBLISHED ON: JANUARY 16, 2023

Cat command in Linux with examples 

The "cat" command on Linux is a versatile command that is used to display and concatenate the contents of one or more files in the terminal. It is considered to be one of the most basic and frequently used commands on Linux, and is often used in conjunction with other commands to perform a variety of tasks.

The "cat" command can be used to view the contents of a single file, concatenate the contents of multiple files and display them in the terminal, view the contents of a file starting at a specific line, and even concatenate files in reverse order. The command is particularly useful for troubleshooting and debugging, as it can be used to display the contents of any file, including binary files.

While the contents of binary files are not human-readable, they can be useful for identifying problems or understanding how a file is structured. Overall, the cat command is a powerful tool that can be used for a variety of tasks and is considered to be a must-know command for any Linux user.

In Linux, the cat (concatenate) command is often used. It reads data from a file and outputs its contents. It assists us in the creation, viewing, and concatenation of files. So, let's look at some often-used cat commands.

1. To view a single file:

The cat command is a Unix utility that is used to concatenate and display files. It is often used to view the contents of a single file, although it can also be used to display multiple files at once. To view the contents of a single file, you can use the cat command followed by the name of the file you want to view.

 $cat filename 

cat command to view file

2. To view multiple files:

The cat command can be used to view the contents of multiple files at once. To do this, simply specify the names of the files you want to view as arguments to the cat command. For example:

$cat file1 file2 

Output:

cat command to view multiple file

3. To view the contents of a file, preceding with line numbers:

To view the contents of a file with line numbers preceding each line, you can use the cat -n command. For example:

$ cat -n filename 

This will print the contents of the file with a line number preceding each line. The line numbers will be left-aligned and will start at 1 for the first line of the file.

$cat -n filename 

Output

cat command

4. Create a file:

To create a new file using the cat command, you can use the cat command in conjunction with the > operator to redirect the output to a new file. For example:

$ cat > newfile.txt 

This will create a new file called newfile.txt and open it in your default text editor. You can then type in the contents of the file and save it when you are finished. You can also use the cat command to create a new file and populate it with the contents of one or more existing files. For example:

$ cat > newfile 

cat commanf

5. Copy the contents of one file to another file:

To copy the contents of one file to another file, you can use the cat command in conjunction with the > operator to redirect the output of the cat command to a new file. For example:

$ cat sourcefile.txt > destinationfile.txt 

This will create a new file called destinationfile.txt and copy the contents of sourcefile.txt into it.

Command:

$cat [filename-whose-contents-is-to-be-copied] > [destination-filename] 

cat command

Keep in mind that using the cat command to create a new file will overwrite the file if it already exists. To avoid overwriting an existing file, you can use the >> operator instead of the > operator to append the contents of the file to the end of the destination file.

6. Cat command can suppress repeated empty lines in the output:

the cat command has an option that allows you to suppress repeated empty lines in the output. The -s option will "squeeze" multiple consecutive empty lines into a single empty line in the output.

$ cat -s file.txt

This will print the contents of file.txt, with multiple consecutive empty lines squeezed into a single empty line.

$cat -s testfile 

Output

cat

Keep in mind that the -s option will only affect consecutive empty lines in the output. If a file contains multiple non-empty lines separated by empty lines, the -s option will not affect the output.

7. Cat command can append the contents of one file to the end of another file:

you can use the cat command to append the contents of one file to the end of another file. To do this, you can use the cat command in conjunction with the >> operator to redirect the output of the cat command to the end of an existing file.

$cat file1 >> file2 

Output

cat command

Will append the contents of one file to the end of another file

8. Cat command can highlight the end of the line:

The cat -E command is used to display the contents of a file with a $ character at the end of each line. This is useful for identifying the end of each line in a file, particularly when the file contains lines that are too long to fit on the screen.

$cat -E "filename" 

Output

cat command

9. If you want to use the -v, -E, and -T options together, then instead of writing -vET in the command, you can just use the -A command line option:

The cat -A command is used to display the contents of a file with various non-printing characters displayed as escape sequences. This is useful for identifying and interpreting non-printing characters in a file, such as tab and newline characters.

$cat -A "filename" 

cat command

Will show that much content, which could fit in the terminal, and will ask to show more.

10. Cat command to write in an already existing file:

The cat >> command is used to append the contents of one or more files to the end of an existing file. The >> operator redirects the output of the cat command to the end of the specified file, rather than overwriting it.

$cat >> geeks.txt 

The newly add ed text.

Output

cat command

Will append the text "The newly added text." to the end of the file.

Conclusion

The "cat" command is a powerful and versatile tool in Linux for viewing and manipulating files. It can be used to view the contents of a single file or multiple files at once, as well as create and modify files. The examples provided demonstrate just a few of the many ways in which the "cat" command can be used. Whether you are a beginner or an experienced Linux user, learning and understanding the "cat" command can greatly enhance your ability to work with files on the command line.



About the author:
Pradeep has expertise in Linux, Go, Nginx, Apache, CyberSecurity, AppSec and various other technical areas. He has contributed to numerous publications and websites, providing his readers with insightful and informative content.