The cat command in Linux is a simple yet powerful tool that is used to view and manipulate text files. It is short for “concatenate,” which means to combine or link together. The cat command has a variety of uses, from displaying the contents of a file to combining multiple files into a single file.
In this article, we’ll cover 11 practical examples of the cat command in Linux, so you can get the most out of this versatile tool.
Syntax
The syntax for the cat command in Linux is as follows:
1 | cat [OPTION]... [FILE]... |
Where:
[OPTION]
is an optional argument that modifies the behavior of cat. Some common options include:-n
: displays line numbers-v
: displays non-printable characters-e
: displays end-of-line characters as $-T
: displays tabs as ^I
[FILE]
is the name of the file you want to view or manipulate. You can list multiple files to concatenate them, or you can use a-
to read from standard input.
11 Practical Examples of Linux `cat
` Command
Here are the 11 practical examples of the Linux cat command.
- Display the contents of a file
The most basic use of cat is to display the contents of a single file. To do this, simply type cat followed by the name of the file:
cat file.txt
- Concatenate multiple files into a single file
You can also use cat to combine the contents of two or more files into a single file. To do this, simply list the names of the files you want to combine, separated by spaces:
cat file1.txt file2.txt > combined.txt
- Display the contents of multiple files
If you want to display the contents of multiple files at once, you can simply list the names of the files after the cat command:
cat file1.txt file2.txt
- Display line numbers
By default, cat does not display line numbers. However, you can use the -n option to display line numbers:
cat -n file.txt
- Display non-printable characters
By default, cat does not display non-printable characters, such as control characters or whitespace. However, you can use the -v option to display these characters:
cat -v file.txt
- Display end of line characters
If you want to see the end of line characters in a file, you can use the -e option:
cat -e file.txt
- Display tabs as ^I
To display tabs in a file as ^I, you can use the -T option:
cat -T file.txt
- Display only specific lines
You can use cat to display only specific lines from a file. To do this, you’ll need to use the head and tail commands in combination with the cat. For example, to display the first 10 lines of a file:
head -n 10 file.txt | cat
- Copy the contents of a file
You can use cat to copy the contents of a file to another file. To do this, simply use the > operator to redirect the output of cat to a new file:
cat file1.txt > file2.txt
- Append to an existing file
To add new content to an existing file, you can use the >> operator to append the output of cat to the end of the file:
cat new_content.txt >> file.txt
- Combine files using pipes
Finally, you can also use pipes to combine the output of multiple cat commands. For example, to combine the contents of two files and then display the result:
cat file1.txt file2.txt | cat
These are just a few of the many ways you can use the cat command in Linux.
Conclusion
In conclusion, the cat command is a simple yet powerful tool that is an essential part of any Linux user’s toolkit. Whether you’re looking to quickly view the contents of a file, combine multiple files, or copy files from one location to another, cat is a versatile and powerful tool that’s worth mastering. By understanding and utilizing the 11 practical examples outlined in this article, you’ll be well on your way to becoming a pro at using the cat command in Linux.