- Cat and Tac Command Usage in Linux
- Tutorial Details
- Reading a File
- Creating a File
- Creating a File Without Removing Old Content
- Reading Multiple Files
- Reading All Files in the Directory Sharing Similar Extensions
- Redirect Output to a New File
- Redirect Output of All Files in Directory to New File
- Remove Multiple Spaces
- Using More and Less Options
- Print the Line Number in the File
- Print $ at the End of the Line
- Sorting File Content
- Print the File Content in Reverse Order
- Linux Cat Command Examples
- How Does the Cat Command Work?
- Example 1: Display the Content of All Files
- Example 2: Display Multiple Files
- Example 3: Copy the Output of One File to Another
- Example 4: Append the Output of a File to Another File
- Copy Multiple Files to Another Text File/Concatenate the Files
- Example 5: Display the Line Numbers in File
- Example 6: Create a File
- Example 7: Sort the Output
- Example 8: Remove the Consecutive Empty Lines
- Example 9: Display the Tab Characters
- Example 10: Print the Output of a File
- Conclusion
- About the author
- Syed Minhal Abbas
- Display all files from a directory with cat at the same time
- 2 Answers 2
Cat and Tac Command Usage in Linux
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 |
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:
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.
Linux Cat Command Examples
In Linux, the “cat” is the concatenation of files which combines multiple files into a single file. There are other several uses of the cat command in Linux which we will talk in this article to give you an understanding of how it works in different scenarios.
- How Does the Cat Command Work?
- Display the Content of All Files
- Display Multiple Files at Once
- Copy the Output of One File to Another File
- Append the Output of a File to Another File
- Display the Line Numbers in the File
- Create New Files
- Sort the Output
- Remove the Consecutive Empty Lines
- Display the Tab Characters
- Print the Output of a File
Let’s start the article with the cat command.
How Does the Cat Command Work?
Using the “cat” command, you can create a file, view the file content, concatenate the files, and redirect the file output. The syntax of this command as follows:
Use the previous command if you are present in the same directory. Otherwise, mention the path to that file as follows:
Different options of the cat command are listed in the following:
Options | Description |
---|---|
-n | To display the line number of file content |
-T | To display the tab-separated characters in a line-e |
-e | To display the “$” at the end of lines |
-s | You can omit the empty lines from the output. |
-a | To display all the file content |
To explore more options, use the following “help” utility:
Example 1: Display the Content of All Files
The common usage of the cat command is displaying the file contents. To display the file contents to a Terminal, simply type “cat” and the filename as follows:
To display all the files in a current directory, use the wildcard character with the cat command as follows:
To display only the contents of the text files in a directory, enter the following command:
Example 2: Display Multiple Files
You can also combine and display the contents of multiple files together in the Terminal using the cat command. To display multiple files simultaneously, use the following syntax:
Example 3: Copy the Output of One File to Another
It can also be utilized to copy the output of one file to another file. If not found, it first creates it. Otherwise, it overwrites the targeted file.
To copy the output of a source file to another file, use the following syntax:
An example of this is to copy the output of a testfile1 to another file named testfile_backup as follows:
This command first creates the testfile_backup file and then copies the contents of testfile1 to it.
Example 4: Append the Output of a File to Another File
Instead of overwriting the output of a targeted file in the pervious example, you can also make the cat command to append the output:
It creates the destination file if it does not already exist. Otherwise, it appends the output.
Copy Multiple Files to Another Text File/Concatenate the Files
The cat command combines several files into a single file. The following syntax can be used to concatenate file1, file2, and file3 and save them to another file named file4.txt:
For instance, we want to concatenate the output of /etc/hostname, /etc/resolv.conf, and the /etc/hosts file to another file named network.txt:
Example 5: Display the Line Numbers in File
To display the line numbers to the output of a file, simply use the -n flag as follows:
For instance, if you are viewing a file which contains the list of items, you can use the -n flag to display those items with a number. Remember that empty lines are also numbered as follows:
If you do not want to number the empty lines, use the -b flag as follows:
Example 6: Create a File
To create a file through the cat command, the following syntax can be used for the purpose:
After entering the previous command, enter the text that you want to store in the file. Once done, save and exit. After that, you can view the contents of your newly created file by executing the following command in the terminal:
Example 7: Sort the Output
You can also combine the sort with the cat command to sort the output alphabetically as follows:
Example 8: Remove the Consecutive Empty Lines
Sometimes, the file contains consecutive empty lines that you do not want to print. The cat command allows merging those consecutive empty lines and shows them as one empty line:
Use the following command syntax to remove the repeated empty lines:
For instance, we have the following file with consecutive empty lines:
Example 9: Display the Tab Characters
Sometimes, you have to remove the tabs from your files. Cat command can help you to find the tabs on your file using the -t flag as follows:
Example 10: Print the Output of a File
Another popular use of the cat command is in the printing contents of a document. For instance, to print the output of a file to a printing device named /dev/lp, the following syntax is used:
Conclusion
In this article, we explained through various examples on how you can use the cat command to manipulate the files in Linux. The cat command is popular among all users because of its simple syntax and the various options that it provides. Creating and viewing a file, merging, copying, and appending the file contents, printing, and a lot more can be handled with this single cat command.
About the author
Syed Minhal Abbas
I hold a master’s degree in computer science and work as an academic researcher. I am eager to read about new technologies and share them with the rest of the world.
Display all files from a directory with cat at the same time
I’m writing a script that would allow me to view the content of every file in an entire selected directory but I don’t quite know how to do it. I was thinking about piping the output of ls into cat or less but that doesn’t seem to work. Is there a command line variable I need to use or are there commands that will satisfy my need? I want this so I can take a quick peak at the contents of files whenever I’m looking for a certain file I can’t remember.
2 Answers 2
This is exactly what cat is for. If you want to display the content of all files in your directory, use
The shell will expand * to the list of files. If you want to include hidden files as well, use
However, if you just want to quickly identify a file, this might not be the best idea, especially if the files are long.
Using head might help in this case:
will display the first ten lines of each file in the directory, with a heading line giving the name of the file.
awesome! thank you so much! i didnt know that arg was available. and ill keep that in mind for the future, right now most of my files are about 10 lines or less
See «Why *not* parse `ls`?» for why parsing the output of ls may not be a good approach.
#!/bin/sh dir="$1" PAGER="$" test -d "$dir" || exit 1 find "$dir" -type f -exec "$PAGER" <> \;
This will take the first command line argument and assign it to the variable dir . The variable PAGER will be set to cat if the user hasn’t already set it to something else.
If the given directory is a valid directory name, find is used to pass all regular files (including hidden regular files) in the directory, or any of its subdirectories, to the pager.
To limit the search of files to only the first level of the given directory, insert -maxdepth 1 before the -exec option to find .
$ PAGER=less ./script "$HOME/stories"
#!/bin/sh dir="$1" suf="$2" PAGER="$" test -d "$dir" || exit 1 find "$dir" -type f -name "*$suf" -exec "$PAGER" <> \;
This will allow the script to take an additional filename suffix like