Introduction
In Linux, the cat command, short for “concatenate,” is primarily used for reading, displaying, and merging the contents of files. This blog will guide you through the various features and capabilities of the cat command, from basic usage to more advanced techniques.
Basic syntax of cat command
The basic syntax of the cat command is as follows:
cat [OPTION] [FILE]
- [OPTION]: Optional flags that modify the behavior of the command.
- [FILE]: One or more filenames whose contents you want to display or concatenate.
The cat command offers several options to customize its behavior when working with files. Here are some commonly used ones:
- -A: This displays all characters, including non-printable ones like tabs and newlines. They are usually shown with special markers for better understanding.
- -b: This adds line numbers, but only for non-blank lines. This helps identify specific sections of the file.
- -E: This shows a $ character at the end of each line to indicate the line ending.
- -n: This numbers all lines, including blank ones. This provides a complete line count.
- -s: This squeezes multiple blank lines into a single blank line. This can be useful for cleaning up files with excessive whitespace.
- -v: This displays various non-printing characters with escape sequences. Similar to -A but with different formatting.
- -T: This shows tabs as ^I instead of the actual tab character.
These are just some of the options available. You can combine multiple options for specific formatting needs.
Some cat command Examples
Displaying a Single File
To display the content of a single file, simply specify the filename:
cat Example1.txt
Combining Multiple Files
To concatenate the contents of multiple files and display the result, list the filenames separated by spaces
cat Example1.txt Example2.txt
Using Options
The -n option adds line numbers to the output
cat -n Example1.txt
The -v option display non-printing characters. When working with text files, especially those generated by other programs or received from external sources, you may encounter non-printing characters that can cause issues such as ^M (carriage return), ^I (Tab), or ^L (form feed) are not visible in the standard output. The -v option makes these characters visible, allowing you to see exactly what’s in your file.
cat -v sample1.txt
The -s option consolidates multiple consecutive blank lines into a single blank line in the output. This is helpful for improving the readability of the file content by removing redundant empty lines. Configuration or system files with many blank lines can be difficult to read. The -s option can make them more compact and easier to navigate.
cat -s /var/log/syslog
Redirecting Output
Redirecting output to a new file is a powerful feature of cat command. This is useful when you need to save the output of cat to a file rather than displaying it on the terminal. Here are the common scenarios:
Redirecting Output to a New File
To save the combined or displayed content to a new file, you can use the ‘>‘operator. This will create a new file if it doesn’t exist or overwrite an existing file with the same name.
cat file1.txt file2.txt > combined.txt
In this example, the contents of file1.txt and file2.txt are combined and saved to combined.txt. If combined.txt already exists, it will be overwritten.
You can also use cat to quickly create new files and add initial content to them. This method is especially handy for scripting or when setting up configuration file.
cat > newfile.txt
This is the first sentence
This is the second sentence
^C
cat newfile.txt
This is the first sentence
This is the second sentence
Appending Output to an Existing File
If you want to add the output of cat to the end of an existing file without overwriting it, use the ‘>>‘ operator.
cat file1.txt >> existingfile.txt
Here, the content of file1.txt is appended to the end of existingfile.txt. This is useful for adding new content to logs or accumulating data over time.
System administrators often redirect command output to log files for future reference or debugging. Appending to log files ensures that new entries do not overwrite previous logs.
cat error_log.txt >> system_logs.txt
Viewing Large Files
The cat command outputs the entire file at once, which might not be practical for very large files. Instead, combining cat with other commands can provide a more manageable way to view and navigate through large files.
Using cat with less
When you pipe the output of cat into less, you can navigate through the content more conveniently and more memory efficient.
cat largefile.txt | less
This command will display the contents of largefile.txt one page at a time. You can scroll through the content using the arrow keys, Page Up and Page Down key. To exit, press ‘q’.
Using cat with more
Similar to less, the more command is another paging utility that can be used with cat to view large files.
cat largefile.txt | more
This command will display the content of largefile.txt one page at a time, and you can navigate using the spacebar to move to the next page and ‘q’ to quit.
Combining cat with Other Commands
The cat command is highly versatile and can be combined with other commands to perform more complex tasks.
Using cat with grep
The grep command is used to search for specific patterns within files. By combining cat with grep, you can search for text within the content of multiple files.
cat Example1.txt | grep 'enim'
This command will display all lines from Example1.txt that contain ‘enim’.
Using cat with sort
The sort command sorts the lines of a file. When combined with cat, you can concatenate multiple files and sort their combined content.
cat Example1.txt Example2.txt | sort
This command will concatenate Example1.txt and Example2.txt, then sort the combined output.
Using cat with awk
The awk command is a powerful text processing tool. Combining cat with awk allows you to perform sophisticated text manipulations. In this example, we extract and process specific columns from a CSV file
cat data.csv | awk -F, '{print $1, $3}'
This command will print the second and third column of each line in data.csv. The ‘-F,‘specifies that the field separator is a comma.
Using cat with wc
The wc command counts lines, words, and characters in a file. Combining cat with wc provides detailed file statistics.
This example count the total number of lines across multiple files:
cat file1.txt file2.txt | wc -l
Using cat with tee
The tee command reads from standard input and writes to both standard output and one or more files. Combining cat with tee allows you to view the output and simultaneously save it to a file.
cat filename.txt | tee output.txt
This command will display the contents of filename.txt and write them to output.txt.
Using cat with head and tail
The head and tail commands display the beginning and end of a file, respectively. Combining cat with these commands allows you to view specific sections of a file.
cat Example1.txt | head -n 2
This command will display the first 2 lines of Example1.txt.
Here is the example using tail:
cat Example1.txt | tail -n 2
This command will display the last 2 lines of Example1.txt.
Conclusion
To sum up, the cat command is a fundamental and versatile tool in Linux. It allows you to efficiently display, combine, and manage file contents. By mastering its various options and integrating it with other commands, you can streamline your workflow and enhance your command line skills.