The Linux terminal is by far the most powerful and effective way of doing just about anything on your Linux machine. There are commands and utilities for doing anything from checking up on files to manipulating processes running on your computer.
The cat command is one of the most popular commands used on Linux systems. In this article, we’re going over what the cat command is and how can you use it on your Linux machine.
What is the cat command?
The cat command is used to view file contents and merge multiple files by appending them into one another. You can also use this command to create new files.
The basic syntax for the command is also rather easy. Type cat followed by the file you want to view.
The above command will show the contents of the text file named file1.
Also read: How to find large files in Linux?
Using the cat command in Linux
Aforementioned, the basic cat syntax is as follows.
cat [flag] [filename]
How to view files with cat command in Linux?
To view the contents of a file, use this command.
Here are a few flags you can use the command when viewing files.
- >: Using this flag along with a second file’s name will copy one file’s contents into the specified file. If used with a single file name, it’ll create the file.
- >>: Using this flag along with a second file’s name will append one file’s contents into the specified file.
- -n: Using this flag will show line numbers in the file.
- -s: Using this flag will suppress repeating empty lines.
- -T: Using this flag will show tab characters as ^I instead of a tab space.
- -e: Using this flag will show you the end of the line as $.
- | more/ | less: These are parameters you can use with the cat command to show more or fewer lines when printing onto the terminal.
Also read: How to use Journalctl to read Linux logs?
How to view multiple files in cat
To view multiple files at the same time, simply type their names one after another.
cat file1.txt file2.txt
If you want to append these files together in a third file, use this command.
cat file1.txt file2.txt > Combofile.txt
If Combofile.txt exists, the command will overwrite the contents of that file with the combined contents of file1 and file2. Otherwise, it’ll create a new file with the combined contents of the two files.
How to create files with cat command in Linux?
As mentioned above, you can create files with cat using the > flag.
cat > NewFile1.txt
After this, type the new file contents and press Ctrl + D to save your changes. Using the cat command to create smaller files is often more convenient than using other programs such as nano, Sublime Text or any other text editor.
Also read: How to open task manager in Linux?