
The cat command, short for "concatenate," is one of the most fundamental utilities in Unix-like operating systems. First introduced in Version 1 Unix (1971), it comes pre-installed on nearly all Linux distributions. While it is most commonly used to display file contents in the terminal, it also supports merging files, creating or appending content, and redirecting output, making it a versatile part of everyday shell scripting and file manipulation.
This article explains how to use the cat command in a variety of practical scenarios. It covers common use cases like viewing files, joining multiple files, redirecting input and output, numbering lines, and working with pipes. You'll also explore useful flags and options to make your usage of cat more efficient and adaptable across different environments.
Before you begin, ensure you have:
sudo privileges.nano to create and modify configuration files.cat Command OptionsThe cat command is a powerful and flexible utility in Linux. It allows you to view, concatenate, and manipulate file contents. The name cat stands for concatenate, reflecting its original purpose of merging files into a single output stream. While many users rely on cat to display files in the terminal, its options enable a variety of additional use cases, such as formatting, debugging, and stream editing.
This section explains the command syntax, describes common flags, and demonstrates practical examples.
The cat command derives from the word concatenate, emphasizing its design to merge file contents into a single output. While concatenation remains its core functionality, cat is commonly used to display files, inspect content, and pipe data between commands.
The general syntax of the cat command is:
[options]: Flags that modify cat's behavior. Use these to change output formatting or processing logic.
[file(s)]: One or more files to process. If omitted, cat reads from standard input.
When you run cat without arguments, it enters interactive mode. Type input and press Ctrl + D to end. This behavior is useful for quick notes or piping input manually.
The table below summarizes the most commonly used cat options.
The cat command supports several options that enhance its functionality. Below examples show how they work in practice.
Create and open a file named example.txt:
Add the following content to the file:
Save and exit using Ctrl + X, then press Y and Enter.
cat Options-b: Numbers only non-empty lines. Useful for code or logs where spacing matters less than content.
Output:
-s: Squeezes multiple consecutive blank lines into a single one. Helps tidy up cluttered files.
-E: Adds a $ at the end of each line to highlight line endings or trailing whitespace.
Output:
-T: Shows tab characters as ^I, making it easier to spot indentation issues. You can create a file with tabs like this:
Output:
-A: Combines -E, -T, and -v to show line endings, tabs, and non-printable characters all at once.
Output:
-v: Reveals non-printing characters like control codes. For example:
Output:
cat CommandThe cat command is most commonly used to display the contents of files directly in the terminal. It's a simple and fast way to view text files such as logs, scripts, or configuration files without opening a text editor. This section demonstrates how to display single files, multiple files, and efficiently view large files.
Create a file named tutorial.txt with a single line of content:
Display the contents of tutorial.txt:
Output:
Display multiple files by listing them after the cat command. For example, to view example.txt followed by tutorial.txt:
Output:
The display order depends on the sequence of file names. To reverse it, use the tac command instead of cat. The tac command is simply cat spelled backward. It displays files line-by-line in reverse order.
Use a wildcard (*) to display all .txtfiles in the current directory:
To read .txt files from a specific directory, you can specify the path to the directory before the wildcard character.
Replace /path/to/directory/ with the actual file path.
To view large files without flooding the terminal, combine cat with other commands using pipes.
Pipe cat with head to read from the top of the file. For example, to read the first 20 lines of a file named largefile.txt :
Similarly, pipe cat with tail to read from the end of the file.
Replace largefile.txt with a large file of your choice.
You can also use the less command to view the contents in a scrollable window.
catTrue to its "concatenate" namesake, cat excels at merging files into unified outputs. This capability is invaluable for log analysis, configuration management, and batch text processing. Explore workflows ranging from basic merging to advanced stream manipulation below:
Merge two files into a single file. The content would be arranged in the new file in order of how the files were listed using the cat command. In this example, merge example.txt to tutorial.txt and write to result.txt.
Verify the content of result.txt.
Output:
The > redirects the merged output to the new file result.txt while keeping the original files intact.
You can combine files using the wildcard (*) to select multiple files and save them into a new file. For example, to combine all files in the directory that end with .txt into result2.txt:
This would also create the file result2.txt if it doesn't exist. To verify the content of result2.txt:
Output:
You can also insert separators or any text between files you want to combine. For example, you can insert a divider line between example.txt and tutorial.txt as they appear in the new file result3.txt.
To verify the content of result3.txt:
Output:
catWhile cat is typically used to display file contents, it can also accept input directly from the terminal and write it to a file. This makes cat a quick way to create or append content without using a text editor, especially useful on headless systems.
Create a new file named file1.txt by redirecting input interactively:
Type the following content when prompted:
Press Ctrl + D to save and exit.
Verify the contents of file1.txt:
Output:
Append content to file1.txt using the >> redirection operator:
When prompted, add the following line:
Press Ctrl + D to finish. Then verify the updated contents:
Output:
Write multi-line templates using a here-document (<<EOF):
View the contents of file2.txt:
Output:
Inject shell variables into files using cat. First, store the current date in a variable:
Append the timestamp to file2.txt using here-strings (<<<):
You can use cat <<< "text" to mimic echo, but echo is more efficient for single-line output. This example uses cat to stay consistent with the rest of the article.
Verify the final contents of file2.txt:
catLine numbering is essential for debugging scripts, analyzing logs, or collaborating on code. The cat command provides two streamlined options (-n and -b) to add line numbers directly in the terminal, eliminating the need for external tools. This section demonstrates how to implement both methods and their practical applications in system administration and development workflows.
-nDisplay the contents of /etc/hosts with line numbers:
Output:
Save a timestamped copy of /etc/os-release with line numbers:
Verify the new file:
-bAdding line numbers by combining cat with the option -b. The -b option numbers only non-empty lines, which is useful for focusing on meaningful content in files with many blank lines, such as configuration files or sparse logs.
Print /etc/ssh/ssh_config while ignoring empty lines:
Output:
You can combine cat -n or cat -b with other tools for advanced filtering and formatting.
Search numbered lines for error messages in syslog.
Example Output:
Extract specific lines from example.txt using awk:
Output:
In this article, you explored how to use the cat command to view, merge, and manipulate file content on Linux systems. You learned how to display files, redirect input and output, append content, inject variables, and add line numbers—both interactively and through automation-friendly scripts.
Whether you're managing logs, writing scripts, or debugging configuration files, cat remains a reliable and essential utility in every Linux user's toolkit. For more advanced use cases, refer to the Linux cat man page.
0 Comments
Be the first to comment and share your perspective with the community.