Our first Unix command is called
cat. You may have seen cat used to display text in the terminal, e.g.,
cat something.txt would show the contents of
something.txt. However, that isn’t really
cat’s true purpose. Let’s check the Unix manual (man) pages:
$ man cat | head -n 5 CAT(1) User Commands NAME cat - concatenate files and print on the standard output
(we piped the output of
man cat into
head -n 5 to limit the output to 5 lines).
View an online copy of the cat man page.
So the real purpose of cat is to concatenate files and print them. Exciting stuff. But this might also make you wonder: what is the standard output? Standard output, or
stdout for short, is the default output stream of a program. When you use
puts you are printing to
stdout, and the stream is usually connected to your terminal, so you see text. That doesn’t mean that
stdout has to print to your terminal though – it can be redirected elsewhere. We’ll worry about that later.
We have established what
cat does. Great. Let’s run it!
$ cat ... (nothing happens) ... ... (heat death of the universe eventually happens) ...
cat isn’t doing anything because we didn’t provide any files to concatenate. However, instead of reminding us this,
cat is instead reading from another stream: standard input or
stdin. It’s waiting for us to give it something to concatenate. We can start typing:
$ cat hello world? hello world? hey, stop that! hey, stop that! stop copying me! stop copying me!
cat will do its best impression of your younger sibling during a 10-hour drive across country.
Here, we saw
cat reading from
stdin and writing to
Moving on, let’s have
cat read from a file instead (while still outputting to
$ cat file.txt This is what's inside file.txt! Incredible, right?
I wonder if…
$ cat file.txt file.txt This is what's inside file.txt! Incredible, right? This is what's inside file.txt! Incredible, right?
Ok, so we can even concatenate a file with itself. Create a few text files,
c.txt like so (the
<<EOM ... EOM syntax below is called a heredoc):
$ cat > a.txt <<EOM Start writing a message here '>' or 'heredoc>' might appear; That's completely fine, keep writing. Whatever you write will be written to 'a.txt' -- we *redirected* the output of cat to a.txt with the '>' symbol above. When you are done writing, you need to type 'EOM' on its own line like this: EOM $ cat > b.txt <<EOM Hello world! EOM $ cat > c.txt <<EOM Great, one more file. Just what we need. EOM $ cat > a.txt <<EOM Overwriting 'a.txt' so we don't have so much text to display below :-) EOM
Finally, you can
$ cat a.txt b.txt c.txt Overwriting 'a.txt' so we don't have so much text to display below :-) Hello world! Great, one more file. Just what we need. $ cat c.txt a.txt b.txt Great, one more file. Just what we need. Overwriting 'a.txt' so we don't have so much text to display below :-) Hello world!
So, the order of the command line arguments matters. One more useful thing we can do is add line numbers to the output with the
$ cat -n /etc/passwd | head -n4 1 root:x:0:0::/root:/bin/bash 2 bin:x:1:1::/:/usr/bin/nologin 3 daemon:x:2:2::/:/usr/bin/nologin 4 mail:x:8:12::/var/spool/mail:/usr/bin/nologin
Great, a numbered list of the first 4 users in our system’s
A bit more redirection
We can use what we’ve done so far to create a new file:
$ cat a.txt b.txt c.txt > d.txt $ cat d.txt Overwriting 'a.txt' so we don't have so much text to display below :-) Hello world! Great, one more file. Just what we need.
You’ll see this usage often – concatenate a few files and then write the result out to a new file. If you change
>> then the file will be appended to rather than overwritten. So:
$ cat b.txt >> c.txt $ cat c.txt Great, one more file. Just what we need. Hello world!
The same applies to using the heredoc syntax.
You can see the other features
cat has by perusing the man pages:
One thing to be aware of: it’s generally not great practice to
cat files of unknown origin. If the file has any special control characters that your terminal might interpret as commands, it can cause all kinds of trouble (especially if the file was provide by a malicious user). If you just want to view a file,
more might be better options – they are pagers designed for viewing pages of text.