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 |
Host System and Architecture | Ubuntu 22.10 (x64) |
OS Compatibility | Ubuntu, Manjaro, Fedora, etc. |
Prerequisites | cat, tac |
Internet Required | No |
Discussed Tools in this Article |
Also Read: Grep Command in Linux
Reading a File
Define the path of the text file, and the cat command will output the content.
$ cat /path/to/file.txt
Creating a File
Create a blank new file using the cat command with the redirection symbol.
$ cat > file.txt
If “file.txt” exists in the present directory, the above command will remove its content.
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 >> file.txt
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
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
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.
$ cat file1.txt file2.txt >> newfile.txt
If “newfile.txt” is already present in the current directory, then new content will be appended.
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.
$ cat *.txt >> newfile.txt
If “newfile.txt” already exists in the present directory, then new content will be appended.
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
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
1 HELLO
2 Linux
3 TLDR
4 READER
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
HELLO$
Linux$
TLDR$
READER$
This is new line in the file$
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
1
2
3
4
5
6
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
line number six
line number five
line number four
line number three
line number two
line number one
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.