This is just a brief overview of the cat command. There are all sorts of ways to use the cat command, and I’m just going to cover some of the most common ways – the ways you’re most likely to use the command in day to day activities.
You’ll find ‘cat’ popping up in various tutorials and may find it being used for all sorts of things. As a command, it has existed since the earliest days of Unix (about 50 years ago, as of the time of this writing) and it gets used still today. If you check the man page, you’ll see that cat describes itself as:
cat – concatenate files and print on the standard output
But I find the Wikipedia description to be even better, at least more descriptive:
cat is a standard Unix utility that reads files sequentially, writing them to standard output.
If you’re curious, ‘concatenate’ just means linking stuff together in a chain or series – but ‘cat’ is more frequently used just to read the contents of a file by writing them to the standard output.
You can use this output in a number of ways, though this article aims to be just a fairly brief article, just an overview of the cat command. I haven’t written it yet, but i expect it to be reasonably brief!
An Overview Of The Cat Command:
This article requires an open terminal, like many other articles on this site. If you don’t know how to open the terminal, you can do so with your keyboard – just press
Next, I want you to run the following commands:
1 2 | cd Downloads wget https://linux-tips.us/files/rnd-num.txt |
That just downloads a text file with some random numbers in it. It’s harmless. Now, let’s view it in the terminal:
1 | cat rnd-num.txt |
If you don’t want to display all the results at once, just try piping it to the ‘less’ command. You can do that because cat handily outputs to the standard output. It’s just:
1 | cat rnd-num.txt | less |
Alright, that was fun! Let’s try something nifty.
1 | cat rnd-num.txt > new-file.txt |
That will take the output of the cat command and add it to a document that you just created at the same time. So, if you want to see your handiwork, just try this command:
1 | cat new-file.txt |
You’ll see the contents of ‘rnd-num.txt’ are now in the new file. If you use > it overwrites the contents of the file, but if you use >> it appends the content to the new file. To test that again, try this:
1 | cat rnd-num.txt >> new-file.txt |
Now, if you cat the ‘new-file.txt’ you’ll see that it has twice as many lines as it did have. If you want to see it for certain, cat will helpfully add line numbers! Try this:
1 | cat -n rnd-num.txt |
Then run (and it should show 100 lines):
1 | cat -n new-file.txt |
Or, for even more fun, you can actually combine those (or just show the output from multiple files without the -n flag) with this:
1 | cat -n rnd-num.txt new-file.txt |
Anyhow, those are a few examples as an overview of the cat command. Use the man cat
command to learn more about it.
Closure:
And there’s another article! Thanks for reading! For a bonus, you can also use cat like touch and create a new file, like so:
1 | cat example.txt |
When you use ls
you’ll see the new file has been created called ‘example.txt’. Tada! Really, read the man page and learn all the wonderful things you can do with the cat command. This has been just a brief overview of the cat command, and there’s so many more nifty things that can be done.
Thanks for reading! If you want to help, or if the site has helped you, you can donate, register to help, write an article, or buy inexpensive hosting to start your own site. If you scroll down, you can sign up for the newsletter, vote for the article, and comment.