Linux cat command FAQ: Can you share some examples of the Unix/Linux cat command?
The Linux cat command lets you view text files. If you're familiar with MS-DOS, the cat command is similar to the DOS
However, the cat command gets its name from the word concatenate, and as that name implies, the Linux cat command can let you merge several files together, as you'll see in the examples below.
In its most basic use, the Linux cat command lets you display the contents of a text file on your screen. For instance, to view the contents of the
/etc/passwd file on your Unix/Linux system, use this command:
If that file doesn't have many lines, it may all fit on your screen, but if the file has many lines, many of them will scroll off the top of your screen as fast as possible. Therefore, many people type a command like this:
cat my-long-file.txt | more
where they use the
more command to keep the contents from scrolling. However, if that's what you want to do, you're better off using the Linux more command like this:
As I mentioned, the name cat comes from the word "concatenate", and the cat command lets you combine several files into one larger file, like this:
cat file1 file2 file3 > file4
This command combines the contents of the first three files into
file4. With this command,
file4 is created if it didn't already exist. (Or, it will overwrite
file4 already existed.)
You can show line numbers when "catting out" a file by using the
-n option, like this:
cat -n myfile.txt
This prints the line number before each line that is output.
You can show non-printing characters with the cat command. The
-T option shows TAB characters, like this:
cat -T myfile.txt
-v option shows all non-printing characters, except for line feed and tab, like this:
cat -v myfile.txt
You can also combine those options, like this:
cat -Tv myfile.txt
or use the lowercase
-t option, which is equivalent to using those flags:
cat -t myfile.txt
For more information, type "man cat" at your Linux command line to get help. Or if you have examples of the Linux cat command you'd like to share, feel free to do so in the comments section below.