Linux FAQ: Can you share some examples of the Unix/Linux cat
command?
The cat
command lets you view text files. If you're familiar with MS-DOS, the cat
command is similar to the DOS type
command.
However, the cat
command gets its name from the word concatenate, and as that name implies, it lets you merge several files together, as you'll see in the examples below.
cat command examples
In its most basic use, the 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:
cat /etc/passwd
Combining the cat and more commands
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 more
command like this:
more my-long-file.txt
Linux cat command: Creating one file from several files
As 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 if file4 already existed.)
Linux cat command: Showing line numbers
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.
Linux cat command: Showing non-printing characters
You can show non-printing characters with the cat
command. The -T
option shows TAB characters, like this:
cat -T myfile.txt
The -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 two flags:
cat -t myfile.txt
More cat command information
For more information, type man cat
at your Linux command line to show the online "manual" for the cat
command. 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.