Networking / Beginners

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.

[Previous] [Contents] [Next]