cat
Description
cat is a standard Unix utility that reads files sequentially, writing them to standard output. The name is derived from its function to (con)catenate files(from Latin catenare, "to chain"). It has been ported to a number of operating systems.
The other primary purpose of cat, aside from concatenation, is file printing i.e. allowing the user to view the contents of a file.
The command syntax is:
cat [flags] [file_names]
The cat utility serves a dual purpose: printing and concatenating
Printing:
With a single argument, it is used to print the contents of a file to the user's terminal.
You can also print multiple files at once by passing multiple file names as arguments to cat. An example is provided in the next section.
Suppose you have a file with namefile.txt with following lines.
This is file.txt.
Let's use cat command to print the content of file.txt.
cat file.txt
Output:
This is file.txt.
Concatenating:
When given multiple arguments, it concatenates the specified files and prints the combined result to the terminal by default. However, you can redirect the result to another file.
Suppose we have two files named file1.txt and file2.txt with following lines.
file1.txt
This is file1.txt
file2.txt
This is file2.txt
Let's use cat command to concatenate file1.txt and file2.txt
cat file1.txt file2.txt
Output:
This is file1.txt.This is file2.txt
Redirection:
cat file1.txt file2.txt > file3.txt
Output:
file3.txt will be created which will contain the output of cat file1.txt file2.txt i.e.This is file1.txt.This is file2.txt.
Note:
>is used to overwrite (“clobber”) a file and>>is used to append to a file. When you usecat file1.txt file2.txt > file3.txt, the output ofcat file1.txt file2.txtwill be written tofile3.txtand iffile3.txtalready exists, its contents will be overwritten.>>is used to append to a file. When you usecat file1.txt file2.txt >> file3.txt, the output ofcat file1.txt file2.txtwill be appended to the contents offile3.txt. Iffile3.txtdoesn't exist, it will create a new file namedfile3.txtwith the output ofcat file1.txt file2.txt, which is equivalent tocat file1.txt file2.txt > file3.txt.
Flags
Some of the popular flags that are used with cat command are as follows:
-n: number all output lines-s: suppress repeated empty output lines.-b: number only non-blank output lines.-v: display nonprinting characters, excepts for tabs and the end of line character.-e: implies-vbut also display tabs as$.-t: implies-vbut also display tabs as^I.
-n
Description:
With the -n option, the cat command will prefix each output lines by its line number.
Usage:
cat -n longtext.txt
Output:
1 Lorem Ipsum is simply dummy t
2
3
4 It has survived not only five
5
6
7 was popularised in the 196
8
9
0 publishing software like Ald
-s
Description:
With the -s option, the cat command will collapse multiple consecutive empty lines into one empty line.
Usage:
cat -s longtext.txt
Output:
Lorem Ipsum is simply dummy t
It has survived not only five
was popularised in the 196
publishing software like Ald
-b
Description:
With -b option, the cat command will number only non-blank output lines ignoring the empty ones.
Usage:
cat -b longtext.txt
Output:
1 Lorem Ipsum is simply dummy t
2 It has survived not only five
3 was popularised in the 196
4 publishing software like Ald
-v
Description:
With -v option, the cat command will display all the non-printing characters with caret and meta notation, except the line feed and tabulation.
With -v option, control characters will appear as a caret(^) followed by the appropriate ASCII characters(e.g., the _carriage_return, byte 13, is displayed as ^M because M in ASCII is 64 +13), and character with the high-order bit set will appear in "meta" notation M- followed by the representation corresponding to the 7 lower bits(e.g., the byte 141 will be displayed as M-^M because 141 is 128 + 13)
Usage:
cat -v cat.jpeg
Output:
M-^?M-XM-^?M-`^@^PJFIF^@^A^A^A^@^@^@^@^@^@M-^?M-a^@.Exif^@^@MM^@*^@^@^@^H^@^B@^@^@^C^@^@^@^A^@d^@^@@^A^@^A^@^@^@^A^@^@^@^@^@^@^@^@M-^?M-[^@C^@
^G^G ^G^F
^H ^K^K
^L^O^Y^P^O^N^N^O^^^V^W^R^Y$ &%# #"(-90(*6+"#2D26;=@@@&0FKE>J9?@=M-^?M-[^@C^A^K^K^K^O^M^O^]^P^P^]=)#)==================================================M-^?M-@^@^Q^H^As^AM-Z^C^A"^@^B^Q^A^C^Q^AM-^?M-D^@^_^@^@^A^E^A^A^A^A^A^A^@^@^@^@^@^@^@^@^A^B^C^D^E^F^G^H
^KM-^?M-D^@M-5^P^@^B^A^C^C^B^D^C^E^E^D^D^@^@^A}^A^B^C^@^D^Q^E^R!1A^F^SQa^G"q^T2M-^AM-^QM-!^H#BM-1M-A^URM-QM-p$3brM-^B
^V^W^X^Y^Z%&'()*456789:CDEFGHIJSTUVWXYZcdefghijstuvwxyzM-^CM-^DM-^EM-^FM-^GM-^HM-^IM-^JM-^RM-^SM-^TM-^UM-^VM-^WM-^XM-^YM-^ZM-"M-#M-$M-%M-&M-'M-(M-)M-*M-2M-3M-4M-5M-6M-7M-8M-9M-:M-BM-CM-DM-EM-FM-GM-HM-IM-JM-RM-SM-TM-UM-VM-WM-XM-YM-ZM-aM-bM-cM-dM-eM-fM-gM-hM-iM-jM-qM-rM-sM-tM-uM-vM-wM-xM-yM-zM-^?M-D^@^_^A^@^C^A^A^A^A^A^A^A^A^A^@^@^@^@^@^@^A^B^C^D^E^F^G^H
-e
Description:
The -e options works like the -v option, except it will also add a dollar sign($) before each line-feed character, thus explicitly showing the end of lines.
Usage:
cat -e longtext.txt
Output:
Lorem Ipsum is simply dummy t$
$
$
It has survived not only five$
$
$
was popularised in the 196$
$
$
publishing software like Ald$
-t
Description:
The -t option works like the -v option, except it will also display tabulations using ^I caret notation.
Usage:
cat -t longtext.txt
Lorem Ipsum is simply dummy t
^I ^I^I
It has survived not only five
was popularised in the 196
^I^I^I
publishing software like Ald
Use cases
Imagine you're on your computer, and you want to quickly check out what's inside a file, maybe it's a funny story or an important message from a friend. You can use the "cat" command to open the file and see its content.
cat story.txtLet's say you're working on a school project and have two different pieces of information in separate files. You can use "cat" to put them together.
cat firstname.txt lastname.txtImagine you're reading a long comic book, and you want to tell your friend about specific pages. You can add page numbers to the comic using this command.
cat -n comic.txt > numberedcomic.txtIf you're starting to write a cool story or a secret journal, you can create a new empty file with this command. It's like getting a blank page to write your thoughts.
cat > newfile.txtIf you're copying a file from one friend's computer to yours, you use this command to transfer the file to your computer.
cat file1.txt > file2.txtImagine you're reading a book with a lot of pages. Instead of flipping through every page, you can use less command to read it one section at a time.
cat chap1.txt chap2.txt chap3.txt | less
Additional Information
A comprehensive discussion of the cat command always includes a reference to the "Useless Use of cat" anti-pattern. In this context, it refers to individuals making tasks unnecessarily complicated when simpler and more efficient methods are available.
Imagine you have a favorite book, and you want to share a cool part with your friend. You could either read the part out loud to your friend, or you could just give them the book to read themselves. Reading it out loud is like using "cat" in a "useless" way because it's extra work.
Here's an example:
Useless Use of cat:
cat favorite_book.txt | less
Simpler and Better Way:
less | favorite_book.txt
In the case of the "Useless Use of Cat," you're essentially vocalizing the book using the "cat" command before passing it on. However, a more straightforward approach is to directly hand over the book to your friend for reading. This method is simpler, avoiding unnecessary extra steps.
Exercises
File Concatenation
- Create three text files, eg.
file1.txt,file2.txt, andfile3.txt, with some sample text in each. - Use the cat command to concatenate the contents of
file1.txt,file2.txt, andfile3.txtinto a single file, let's call it allfiles.txt. - Verify that all
files.txtcontains the combined text from the three original files.
- Create three text files, eg.
Creating and Copying files
- Create a new text file named
mydocument.txtusing the cat command. - Type a short message or any text content into
mydocument.txt. - Create a copy of
mydocument.txtand name itmydocument_copy.txtusing the cat command. - Verify that both
mydocument.txtandmydocument_copy.txtcontain the same text.
- Create a new text file named
Numbering Lines
- Create a text file named
poem.txtwith a few lines of poetry. - Use the
catcommand with the-noption to display the contents ofpoem.txtwith line numbers. - Observe the output and note how line numbers are added.
- Create a text file named