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.
|Description||Cat and Tac command|
|Root or Sudo Privileges||Maybe|
|Host System and Architecture||Ubuntu 22.10 (x64)|
|OS Compatibility||Ubuntu, Manjaro, Fedora, etc.|
Reading a File
Define the path of the text file, and the cat command will output the content.
$ cat myfile.txt
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
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
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.
newfile.txt” is already present in the current directory, then new content will be appended.
$ cat file1.txt file2.txt >> newfile.txt
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
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
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 “
$ cat -e file.txt
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
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
That’s all for now. If you have any more examples, do let us know in the comment section.