Eren Akbulut's Blog

Unix Commands I - cat

January 12th, 2021

Hello everyone, today I'll introduce you a new series of mine about the unix commands. I'll try to mention keep each command as efficient as possible by explaining the most common use cases only ( at least the ones I use the most).

Our series starts with the "cat" command today, I hope you guys will find it useful.


cat

The cat is the short for concatenate and is a Unix utility to read then write files to standard output. The cat is one of the most basic yet popular Unix commands that you'll probably use if you are using a Unix based OS, or if you are managing a server remotely over the terminal.

I'll a couple of examples of the cat use cases.


cat-standard-input

In the example above we created 2 files called test1 and test 2 using one of the cat's features. Using cat without passing any files to read as we did in the example will lead the cat into a mode that it expects from the standard input. Now we can write anything we want to be in the files above, to close cat and save the file we should use the key combination "Ctrl + D".

cat-basic-standartoutput

The use above is the most common use of the cat command basically. It reads a file and writes it to the standard output directly. As you can see the files we created from the example before maintains their content intact. With this use we can also pass multiple files as arguments to the cat, so we can see that the output of test2 comes just after output of test1 ends.

cat-concatenate

Just like we printed what we have in those 2 files we also can create a new file with the same output. As you can see above test3 now contains all we have in test1 and test2.

cat-add-to-existing-one

We also can add the readed content to an existing file rather than creating one. As you can see above we created a file called testAppend with the given content and then added the content of testAppend file into our existing test3 file.

cat-unix-n-flag

-n is one of the 2 cat flags I'll cover in this post because those can be the ones I regularly use. -n flag of cat helps us to follow the lines of the printed content by adding line numbers at the beginning of each of them.

cat-b-flag

We also can combine -n flag with -b flag to output only non blank lines. We also can use the -b flag standalone if you don't need to see line numbers.

You can check the other cat flags from the cat manual both from the official website of unix manuals and from your unix system by running the command "man cat".

We could do other examples using cat and Unix pipes but I think I'll cover that usages after I cover Unix pipes.


That's the end of my coverage about the Unix cat command. That post was a shorter one but I think it could be helpful for many people. Until next time, take care of yourselves. :)

This blog has been created by Eren Akbulut 2020