The Linux cat Command

2:56 PM

This is one of the best articles I've read on how to use the cat command in Linux.

 

cat

Cat is  used to either view, create, or join multiple text files together.  (In fact, the term “cat” is short for catenate, which is a fancy way of saying “to join two things together, end-to-end”.)

By default, stdin for cat is the keyboard, and stdout is the computer screen.  If you just type “cat” at the command prompt, you’ll be able to type in text, and make it echo back to you as soon as you hit Enter.  It will keep doing this until you press Ctrl-d to end it.

cat

My hostname is mail.example.com

My hostname is mail.example.com

Of course, this by itself isn’t terribly useful.  But, you can use cat with the stdout redirector to create simple text files.  When you’re through typing the message, hit Enter once more to get to a blank line, and then press Ctrl-d to exit.

cat > test

My hostname is mail.example.com

Once you’ve created your file, you can now use cat to display it.  It’s not like the less utility, though; cat simply dumps everything in the file onto the display screen.  Note that you don’t need to use a stdin redirector with cat.  That’s because cat is designed to use arguments, instead of stdin redirectors.

cat test

My hostname is mail.example.com

Now, use cat to create a second text file.

cat >test2

Your hostname is ftp.example.com

Here’s where the catenate part comes in.  Invoke cat again, but use the names of both of your new files as arguments.

cat test test2

My hostname is mail.example.com

Your hostname is ftp.example.com

This time, you’ll see both of your files displayed as if they were one single file.

Now, add a stdout redirector, and you’ll be able to create a new file by combining the first two.

cat test test2 >test3

cat test3

My hostname is mail.example.com

Your hostname is ftp.example.com

There are several display options that you can use with cat.    Use the -s option to squeeze out  extra blank lines.  That way, you’ll never have two or more consecutive blank lines.

cat -s  filename

The -t (or -T) option will cause all tabs to be shown as ^I.

If you need to see where the ends of lines are, you can use the -e (or -E) option to mark them.

cat -cat -e /etc/dovecot.conf

protocols = pop3 $

ssl_disable = yes$

protocol imap {$

}$

$

protocol pop3 {$

}$

protocol lda {$

postmaster_address = postmaster@example.com$

}$

auth default {$

mechanisms = plain$

passdb pam {$

}$

userdb passwd {$

}$

To use both the tabs and the end of line options  together, use the -A option.

cat -A /etc/dovecot.conf

The -b option will number all non-blank lines for you.

cat -b /etc/dovecot.conf

1    protocols = pop3

2    ssl_disable = yes

3    protocol imap {

4    }

5

6    protocol pop3 {

7    }

8    protocol lda {

9      postmaster_address = postmaster@example.com

10    }

11    auth default {

12      mechanisms = plain

13      passdb pam {

Or, use the -n option to have all lines numbered.

cat -n /etc/postfix/main.cf

1    queue_directory = /var/spool/postfix

2    command_directory = /usr/sbin

3    daemon_directory = /usr/libexec/postfix

4    mail_owner = postfix

5    inet_interfaces = all

6    unknown_local_recipient_reject_code = 550

7    debug_peer_level = 2

8    debugger_command =

9             PATH=/bin:/usr/bin:/usr/local/bin:/usr/X11R6/bin

10             xxgdb $daemon_directory/$process_name $process_id & sleep 5

11    sendmail_path = /usr/sbin/sendmail.postfix

12    newaliases_path = /usr/bin/newaliases.postfix

13    mailq_path = /usr/bin/mailq.postfix

14    setgid_group = postdrop

15    html_directory = no

16    manpage_directory = /usr/share/man

17    sample_directory = /usr/share/doc/postfix-2.3.3/samples

18    readme_directory = /usr/share/doc/postfix-2.3.3/README_FILES

19    inet_interfaces = all

20

21    # Basic Configuration

via The Linux cat Command.

 

xjonquilx | Sabayon, Ubuntu, Fedora, Linux, Oh My!



You Might Also Like

0 comments