Cat and Tac Command Usage on Linux

Linux TLDR
Last Updated:
Reading time: 3 minutes

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

DescriptionCat and Tac command
Difficulty LevelLow
Root or Sudo PrivilegesMaybe
OS CompatibilityUbuntu, Manjaro, Fedora, etc.
Prerequisitescat, tac
Internet RequiredNo

Reading a File

Define the path of the text file, and the cat command will output the content.

$ cat myfile.txt

Output:

Reading a file using cat command

Creating a File

Create a blank new file using the cat command with the redirection symbol.

📝
If “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 new file using cat command

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:

Creating a new file without removing old content if file already exists

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 the content of multiple files at once

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:

Reading the content of multiple files at onces using special character

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.

📝
If “newfile.txt” is already present in the current directory, then new content will be appended.
$ cat file1.txt file2.txt >> newfile.txt

Output:

Redirecting the multiple output to a new file

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.

📝
If “newfile.txt” already exists in the present directory, then new content will be appended.
$ cat *.txt >> newfile.txt

Output:

Redirecting the multiple output to a new file using special character

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:

Removing the extra line from file for better readibilty

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:

Printing line number for each line in the file

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:

Identifying the end of line using cat command

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:

Sorting file content using cat command

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:

Reverse sort file content using tac command

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.