The cat command in linux – “Who will bell the cat ??”


gnuI have been receiving requests from some of my friends to write blogs on few of the commonly used Linux commands. A lot of tutorials related to Linux commands are available on the internet. The purpose of this blog is to explain the commands in as much detail as possible and also in a practical perspective. Illustration of examples which would make learning easy and interesting for beginners. I would like to start with one of the easiest and commonly used Linux commands, “CAT”.

cat stands for concatenation. The cat command can be used in the following scenarios:

  • To display contents of a file.
  • To concatenate the contents of files.
  • To copy contents of one file to another.

In all the above examples consider a three files named file1.txt file2.txt and file3.txt.

1. To display contents of a file.

 cat file1.txt

The command would display the contents of file1.txt on the shell.
1 2 3

The cat command would throw an error if file1.txt did not exist.

cat -A filename.txt

The above command shows all the special characters at the beginning as well as ending of a file. ( It can be used to verify if there are any special characters before code is checked into the git repository. )

cat -E filename.txt

The above command shows line endings by “$”

cat -n filename.txt

The command shows the contents of filename.txt along with line numbers.

cat -T filename.txt

The command shows all the tab characters by ^I.  (This is very useful to verify the code for tabs before checking into the git repository.)

cat -s filename.txt

The command displays contents of filename.txt after suppressing  all the blank lines.

2. To concatenate the contents of files. 

cat file1.txt > file2.txt

The contents of file1.txt will be copied to file2.txt. If file2.txt does not exist, a new file would be created. If file2.txt already existed, then the contents of file2.txt will be overwritten with file1.txt

cat file1.txt >> file2.txt

Please note the difference between the above example and earlier example. In the above example where “>>” is used, the contents of file1.txt are “appended” instead of “copied” to contents of file2.txt.

For example, if the contents of file1.txt is
1 2 3
and contents of file2.txt is:
4 5 6
contents of file2.txt after execution of above command would be:
4 5 6
1 2 3

cat *

The command above would concatenate the contents of all the files in the current directory.

cat  * > file3.txt

This would concatenate all the files in the current directory, create a new file3.txt and write all the concatenated data to it.  The contents of file3.txt is now

1 2 3

4 5 6

1 2 3

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s