The cat command is pretty useful for reading, creating, and concatenating files. While the tac command also works similarly to the cat command, which outputs the last line first.
Tutorial Details
Description | Cat and Tac command |
Difficulty Level | Low |
Root or Sudo Privileges | Maybe |
OS Compatibility | Ubuntu, Manjaro, Fedora, etc. |
Prerequisites | cat, tac |
Internet Required | No |
Reading a File
Define the path of the text file, and the cat command will output the content.
$ cat myfile.txt
Output:
Creating a File
Create a blank new file using the cat command with the redirection symbol.
myfile.txt
” exists in the present directory, the above command will remove its content.$ cat > myfile.txt
#OR
$ cat /dev/null > myfile.txt
Output:
Creating a File Without Removing Old Content
The following command will create a new file if it’s not exists or hold the content if file already present in the directory without removing any content.
$ cat >> myfile.txt
#OR
$ cat /dev/null >> myfile.txt
Output:
Reading Multiple Files
You can specify as many files as you want to read using the cat command, separating each with a space.
$ cat file1.txt file2.txt
Output:
Reading All Files in the Directory Sharing Similar Extensions
If you have a directory with a bunch of files with the “.txt
” extension, you can easily read all of the files’ content using the following command:
$ cat *.txt
Output:
Redirect Output to a New File
If you want to save the output of two or more files into a new file, use the following command.
newfile.txt
” is already present in the current directory, then new content will be appended.$ cat file1.txt file2.txt >> newfile.txt
Output:
Redirect Output of All Files in Directory to New File
If you are present in a directory with a bunch of “.txt
” files and want to output all files to a new file, use the following command.
newfile.txt
” already exists in the present directory, then new content will be appended.$ cat *.txt >> newfile.txt
Output:
Remove Multiple Spaces
Configuration or programming files contain multiple repeated blank lines that make them larger and more difficult to read, which can be easily replaced with one blank line using the following command.
$ cat -s file.txt
Output:
Using More and Less Options
If your file is too large to read, use the more or less options to display the single screen content of the file by going down one page at a time using the down arrow key.
$ cat file.txt | more
$ cat file.txt | less
Print the Line Number in the File
You can easily get the line number in front of each line, making it easier to find lines in a file, using the following command.
$ cat -n file.txt
Output:
Print $ at the End of the Line
Large lines in file make it difficult to find the end of the line, which can be easily spotted by placing “$
” at the end of each line using the “-e
” flag.
$ cat -e file.txt
Output:
Sorting File Content
If you have numbers in a file arranged in random order, you can easily sort them from ascending to descending using the following command.
$ cat file.txt | sort
Output:
Print the File Content in Reverse Order
If you have a sorted number or want to read the last line first, use the “tac
” command instead of the “cat
” command as shown.
$ tac file.txt
Output:
That’s all for now. If you have any more examples, do let us know in the comment section.
Join The Conversation
Users are always welcome to leave comments about the articles, whether they are questions, comments, constructive criticism, old information, or notices of typos. Please keep in mind that all comments are moderated according to our comment policy.