This article will focus on all useful aspects of “cat” commands. However, this command is also highly suitable for performing some really tricky tasks in scripting.
Cat usage
- “cat” location
The binary is located in the “/usr/bin/cat” location.
- “cat” version
This tool is a part of the GNU coreutils package. The source code of GNU coreutils is readily available on GitHub.
- Display file content
I’ve created a text file with random data. The data was grabbed from random bytes generator by Random.org.
Let’s check out the content of the file using “cat”.
The “cat” tool can print the output of any file that the current user has the permission to read. For example, “/etc/passwd” file is accessible to any user to just “read”.
However, it can’t access something that only “root” has permission to. In this case, the file “sudo_random.txt” is the exact copy of the original “random.txt” but only “root” having access to it.
- Contents of multiple files
The structure of this command is similar to the basic usage of “cat”. All you have to do is pass the files with their location one by one.
It can be performed in a different manner as well.
- Create a file using “cat”
It’s not actually a core function of the “cat” command. However, it can serve the task quite easily.
After running this command, you can type whatever you want and then, press “Ctrl + D”. It will write the user input to the file.
If you want just a blank file, then press “Ctrl + D” without typing anything.
- “more” or “less”
If you’re accessing a file that’s too large, then scrolling through the output of “cat” command becomes really, really annoying. In that case, we can redirect the output to “more” or “less” for more convenience.
For example, the demo text file I’m using is quite big. If you’re working with log files, this is a familiar scenario. In such situations, “more” or “less” can offer significant value. The “more” tool displays the output one page at a time. The “less” tool is similar to “more” but with additional features. However, we’re not going to dive deeper into these tools.
Let’s redirect the output of “cat” to “more”.
To quit the view, press Q.
For pipelining the output to “less”, use this command.
Same as “more”, quit by pressing Q.
- Line numbers
When “cat” displays the content, it doesn’t show the numbering of the lines. Use the “-n” flag.
You can use this with “more” or “less” as well.
When using “-n” flag, “cat” shows line numbering for all the lines, including empty and non-empty ones. However, using the “-b” flag, “cat” will only number the non-empty ones.
Note: This flag will override “-n” by default.
- End of line
How about replacing the “end of line” with $?
Here, “cat” prints the output with both the line number and replacing the “end of line” with $ symbol.
- Display tab
Using the following command, you can swap the tab spaces with “^I” character.
Within the chaos of characters, it’s hard to find out those tabs, right?
- Suppress repeated empty lines
In some cases, there could be multiple empty lines in-between the content. In that case, use “-s” flag to eliminate the empty lines in the output.
- Redirect output
We can use the standard output format to redirect the output of any “cat” command to a file. If the file already exists, it will be overwritten. Otherwise, it’ll be created.
This command can also be used to merge the contents of multiple files into one single file.
If you don’t want to overwrite the content of an existing file, you can append the “cat” output at the end.
Just like before, it’s possible to append the content of multiple files into the same file.
… <fileN> >> <target_file>
- Showing non-printing characters
A text file isn’t just all the showing characters. There are a number of hidden characters that are non-printable. If you need to show them, use the “-v” flag.
“cat” alternative
While “cat” is a crucial part of every single UNIX/Linux system, there are reliable alternatives to print the content of a text file. Here, I’ll be showing off “bat” – a “cat” clone with wings!
The “bat” tool is readily available on all the major Linux distros. It comes up with its own style. You can customize the output with themes, pager, formats and a lot more.
Let’s see how “bat” shows the content of my demo file.
As you can see, “bat” shows line number and the file name by default. Moreover, it uses the “more”-like scrolling by default. For getting out of the window, press Q.
Let’s see if “bat” successfully makes a copy of the file.
Using “bat”, it’s possible to perform all the “cat” functions without any trouble. For complete documentation, check out the official bat GitHub page.
Final thoughts
There are plenty of scenarios where “cat” and “bat” can be useful. For all the available options, there’s nothing better than the man and info pages.
Your creativity is the only limiting factor in terms of unlocking the maximum potential of these tools.
Enjoy!