cat: it is not a cat, it is a concatenator


Well, the cat program it is probably one of the most used in a Linux shell. It is very basic, but also very practical. Sometimes the simplest and most basic tools are also the most powerful in terms of usefulness. Its name comes from conCATenate, hence the title that I have decided to put to this article where I will try to explain it for those who do not know it yet.

Basically the concatenator goes beyond what its name suggests, not only concatenates, it can also display the content of a text file, and combined with the pipes it can be even more powerful and serve multiple uses. And I will summarize all these basic utilities in this article with practical examples, so that you can see it in a more intuitive way ...

For, simply view the contents of a file, you can use the following command:

cat nombre_archivo

cat /etc/passwd


That is going to show you on the screen. In the case of being an encrypted file or a binary, then you will see that it shows you strange characters or symbols without much meaning for you.

You can even view the content of multiple concatenated files in the output, hence its name:

cat nombre_archivo

cat /etc/passwd /etc/passwd- /etc/hosts

Another alternative possibility to touch is use cat to create a file:

cat > ejemplo.txt

This interactively opens a kind of editor in which you can enter all the text you want that file you created to have, or leave it empty. When you have finished entering what you need, just press Ctrl + D to go out.

In case that the content of a file can be long and you prefer to facilitate its navigation and reading, you can use less and more for it:

cat manual.txt | more
cat manual.txt | less

This will make navigating the displayed text much easier. By the way, for exit press Q as you would with man, etc.

Very interesting for developers ands show line number. If you want cat to number the lines of the content of a file use the n option. That will not alter the content of the file, it will only show the numbers in the output:

cat -n codigo.c 

By the way, if you want to save the numbered text as it is shown on the screen, you could do it using redirects. For example:

For, simply view the contents of a file, you can use the following command:

cat -n codico.c > numerado.txt

Remember that with> what you do is sustitute the content of numbered.txt in this case. That is, if it did not exist, it is created with the content, but if it already existed, what was previously in it will be deleted and the new content will be saved. To avoid deleting the above and adding the content at the end, use >> instead of>.

The downside of the -n option is that it numbers all the lines. But if you want prevent those that are empty from being numbered, you can use -b:

cat -b /etc/hosts

Of course, with all the options you can use pipes and redirects to funnel content, etc. And I advise you to read the cat man to learn more. There are options like -e to replace EoL (End of Line) with $, -T for tabs, -s to suppress empty repeating lines, etc.

Until now the visible characters were simply shown, but there are also many other invisible ones that are not visible. To show them use the v flag:

cat -v demo.odt

You can use it to delete the content of files and leave them empty. For that you can use the special device / dev / null like this:

cat /dev/null | demo.txt

In this case the content of / Dev / null it is null and that redirects it through the pipeline to the demo.txt text file. That is, demo.txt is filled with null content, or in other words, it is deleted.

You can also use this other to be able enter sequences until you type EOL and it closes, that is, it opens as a kind of editor in which you can write whatever you want. Obviously, just like that it is useless, but for example, you can write everything and save it to a file:

cat << EOL > hola

Basically << tells the shell that you are going to enter strings until you enter a label, which in this case is EOL, which will indicate that you have reached the end. You can substitute EOL for STOP, or whatever you want.

And if you are working with heterogeneous systems, you may have noticed that the text files of the D worldOS / Windows use other end-of-line characters than UNIX / Linux, so the text may be misaligned when trying to view the file on either system. In * nix LF is used to end the line, while in DOS / Windows CR / LF is used. In order to solve this you have two tools that are dos2unix to convert from DOS / Windows to Unix and unix2dos for the reverse. But if you don't want to install them, you can use cat for it like this:

cat dos.txt | sed "s/^M\{1,\}$//" | sed "$ s/^Z//" > archivo_convertido.txt

cat unix.txt | sed "s/$/^M/" | sed "$ s/$/^Z/" > archivo_convertido.txt

As you can see, cat's profits are many. Despite its simplicity, it allows you to do a multitude of things. Also, everything you have seen here is not the only thing. You can imagine many other utilities and new things for which it could serve you. Remember to read more in the manual:

man cat


The content of the article adheres to our principles of editorial ethics. To report an error click here!.

Be the first to comment

Leave a Comment

Your email address will not be published.



  1. Responsible for the data: AB Internet Networks 2008 SL
  2. Purpose of the data: Control SPAM, comment management.
  3. Legitimation: Your consent
  4. Communication of the data: The data will not be communicated to third parties except by legal obligation.
  5. Data storage: Database hosted by Occentus Networks (EU)
  6. Rights: At any time you can limit, recover and delete your information.