Linux tutorial on the cat command, covering basic and advanced file display with practical examples.
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.
display_file.sh
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.
display_multiple_files.sh
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.
create_file.sh
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.
append_to_file.sh
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.
combine_files.sh
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.
display_line_numbers.sh
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.
non_printable_characters.sh
cat -v filename.txt
The -v option displays non-printable characters in a visible format.
Use for Small Files: Use cat for small files to avoid overwhelming the terminal.
Combine with Other Commands: Use cat with commands like grep or less for advanced processing.
Redirect Output: Use > and >> to create or modify files.
Check File Contents: Use 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.