Utilities

Linux cat Command

The term “cat” stands for “concatenate”. It’s mostly used to display the content of a file in text format. However, we can use “cat” to perform other actions like viewing multiple files, creating a blank file, redirect a file content to other tools, etc.

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
which cat

The binary is located in the “/usr/bin/cat” location.

  • “cat” version
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”.

cat random.txt

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”.

cat /etc/passwd

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.

cat sudo_random.txt

  • 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.

cat <file1> <file2> .. <fileN>

It can be performed in a different manner as well.

cat <file1>; cat <file2>; …; cat <fileN>

  • Create a file using “cat”

It’s not actually a core function of the “cat” command. However, it can serve the task quite easily.

cat > <file>

After running this command, you can type whatever you want and then, press “Ctrl + D”. It will write the user input to the file.

cat > demo.txt

cat demo.txt

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”.

cat <filename> | more

To quit the view, press Q.

For pipelining the output to “less”, use this command.

cat <filename> | less

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.

cat -n <filename>

You can use this with “more” or “less” as well.

cat -n <filename> | more

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.

cat -b <filename>

  • End of line

How about replacing the “end of line” with $?

cat -e -n <filename>

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.

cat -T <filename>

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.

cat -s <filename>

  • 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.

cat <filename> > <target_file>

This command can also be used to merge the contents of multiple files into one single file.

cat <file1> <file2><fileN> > <target_file>

If you don’t want to overwrite the content of an existing file, you can append the “cat” output at the end.

cat <filename> >> <target_file>

Just like before, it’s possible to append the content of multiple files into the same file.

cat <file1> <file2> <file3>
 … <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 -v <filename>

“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.

man cat

info cat

man bat

info bat

Your creativity is the only limiting factor in terms of unlocking the maximum potential of these tools.

Enjoy!

About the author

Sidratul Muntaha

Sidratul Muntaha

Student of CSE. I love Linux and playing with tech and gadgets. I use both Ubuntu and Linux Mint.