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!
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 CTRL + ALT + T and your default terminal should open.
Next, I want you to run the following commands:
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:
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:
cat rnd-num.txt | less
Alright, that was fun! Let’s try something nifty.
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:
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:
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:
cat -n rnd-num.txt
Then run (and it should show 100 lines):
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:
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.
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:
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.
Today we'll cover one way to enable or disable your network interface in the Linux…
Today's exercise is a nice and simple exercise where we check your NIC speed in…
Have you ever wanted to easily monitor your wireless connection? Well, now you can learn…
I think I've covered this before with the ls command but this time we'll count…
Today we'll be learning about a basic Linux command that's known as 'uname' and it…
If you've used hardinfo in the past, it may interest you to know that hardinfo…