last modified February 25, 2025
The cat command in Linux is used to concatenate and display the
contents of files. It is a versatile tool for viewing, creating, and combining
files directly from the command line. This tutorial covers basic and advanced
usage of cat with practical examples.
cat is commonly used for displaying file contents, creating new
files, and appending data to existing files.
This example demonstrates how to display the contents of a file.
cat filename.txt
The cat command outputs the contents of filename.txt
to the terminal.
This example shows how to display the contents of multiple files sequentially.
cat file1.txt file2.txt
The cat command concatenates and displays the contents of
file1.txt and file2.txt.
This example demonstrates how to create a new file using cat.
cat> newfile.txt
The > operator redirects input to newfile.txt. Type
the content and press Ctrl+D to save and exit.
This example shows how to append text to an existing file.
cat>> existingfile.txt
The >> operator appends input to existingfile.txt.
Type the content and press Ctrl+D to save and exit.
This example demonstrates how to combine multiple files into a single file.
cat file1.txt file2.txt> combined.txt
The > operator redirects the concatenated output to
combined.txt.
This example shows how to display file contents with line numbers.
cat -n filename.txt
The -n option adds line numbers to the output.
This example demonstrates how to display non-printable characters in a file.
cat -v filename.txt
The -v option displays non-printable characters in a visible
format.
cat for small files to avoid overwhelming the terminal.cat with commands like grep or less for advanced processing.> and >> to create or modify files.cat to quickly verify file contents before processing.
In this article, we have explored various examples of using the cat
command for displaying, creating, and combining files, including advanced
features like line numbers and non-printable characters.
My name is Jan Bodnar, and I am a passionate programmer with extensive programming experience. I have been writing programming articles since 2007. To date, I have authored over 1,400 articles and 8 e-books. I possess more than ten years of experience in teaching programming.
List all Linux tutorials.