The cat command
The cat command displays the contents of a file. It has the following syntax:
cat [options] [filename...]
Notice that the filename is optional. If you omit the filename, the cat command obtains its input from the console, which you can redirect if you want.
Also notice that you can specify more than one filename. If you do, the files are combined to create a single output stream.
Here are some of the options you can use:
- -A: Displays new line characters as $, tab characters as ^I, and control characters with a caret (^).
- -b: Numbers all nonblank lines as they're displayed.
- -e: Displays new line characters as $ and control characters with a caret (^).
- -E: Displays new line characters as $.
- -n: Numbers lines as they are displayed.
- -s: Squeezes multiple spaces down to a single space.
- -t: Displays tab characters as ^I and control characters with a caret (^).
- -T: Displays tab characters as ^I.
- -v: Shows nonprinting control characters with a caret (^).
Here's a basic example:
$ cat /etc/hosts # Do not remove the following line, or various programs # that require network functionality will fail. 127.0.0.1 LSERVER localhost.localdomain localhost $
If you don't provide any filename arguments, the cat command copies text from the keyboard and displays it on the console. You can use the cat command along with output redirection to quickly create a short text file, like this:
$ cat >mytext This is line one. This is line two. This is line three. <ctrl+D>
For the last line, press Ctrl+D. This signals the end of the input to the cat command.
In this tutorial:
- Linux Commands
- Command Shell Basics
- Editing commands
- Wildcards
- Redirection and piping
- Environment variables
- Shell scripts
- Directory and File Handling Commands
- The cd command
- The mkdir command
- The rmdir command
- The ls command
- The cp command
- The rm command
- The mv command
- The touch command
- The cat command
- Commands for Working with Packages and Services
- The rpm command
- Commands for Administering Users
- The usermod command
- The chage command
- The passwd command
- The newusers command
- The groupadd command
- The groupdel command
- Commands for Managing Ownership and Permissions
- The chgrp command
- The chmod command
- Networking Commands
- The ipconfig command
- The netstat command
- The ping command
- The route command
- The traceroute command