Enter your email address:


Saturday, April 20, 2013

Home » , , » 10 Cat Command Examples to Manage Files in Linux / UNIX

10 Cat Command Examples to Manage Files in Linux / UNIX



Cat command is one of the basic commands that you learned when you started in the Unix / Linux world.

You already know that cat displays a file content. What more could this command do?
This tutorial gives 10 practical usage examples for cat command. Probably few of these examples could be new to you. Or, it might just be a refresher for you, if you already knew these examples.

1. Display the contents of a file

When you pass the filename as an argument to cat, it displays the contents of the file as shown below.
$ cat program.pl
#!/usr/bin/perl

if( 2 ge 3) {
print "greater\n";
}else {
print "lesser\n";
}
You can also display contents of more than one file as shown below.
$ cat program.pl program2.pl
#!/usr/bin/perl

if( 2 ge 3) {
print "greater\n";
}else {
print "lesser\n";
}
#!/usr/bin/perl

@arr = qw(1 2 3);
$ref = \@arr;
print ref $ref;

2. Create a New File

Using cat command, the lines received from stdin can be redirected to a new file using redirection symbols.
When you type simply cat command without any arguments, it just receives the stdin content and displays it in the stdout. So after typed the line once, when you press enter, the same line gets printed in the subsequent line as seen below.
$ cat
cat command for file oriented operations.
cat command for file oriented operations.
cp command for copy files or directories.
cp command for copy files or directories.
You can also redirect the stdout to a new file as shown below.
$ cat >cmd_usage.txt
cat command for file oriented operations.
cp command for copy files or directories.

$ cat cmd_usage.txt
cat command for file oriented operations.
cp command for copy files or directories.
Sometimes you may have to append the content to a file, use >> redirection symbol as shown below.
$ cat >>cmd_usage.txt
ls command to list out file and directory with its attributes.

$ cat cmd_usage.txt
cat command for file oriented operations.
cp command for copy files or directories.
ls command to list out file and directory with its attributes.

3. Copy File Content

Redirection symbols in unix plays an important role in processing the standard file descriptor contents. Using it, you can copy the contents of one file into another as shown below.
$ cat program.pl >backup_prgm.pl
As seen above, since we used the output redirection, the content displayed in standard output gets directed into a new file called backup_pgrm.pl. View the contents of backup_pgrm.pl:
$ cat backup_pgrm.pl
#!/usr/bin/perl

if( 2 ge 3) {
print "greater\n";
}else {
print "lesser\n";
}

4. Concatenate Contents of Multiple Files

Through cat command, you will be able to concatenate contents of more than one file into a new file.
For example, the codes from program.pl and program2.pl gets combined into a new file all_pgrm.pl.
$ cat program.pl program2.pl >all_pgrm.pl
As seen above, stdout gets redirected and the new file has been created with the contents of program.pl and program2.pl. Verify the contents of all_pgrm.pl:
$ cat all_pgrm.pl
#!/usr/bin/perl

if( 2 ge 3) {
print "greater\n";
}else {
print "lesser\n";
}
#!/usr/bin/perl

@arr = qw(1 2 3);
$ref = \@arr;
print ref $ref;

5. Display Line numbers

To display the contents of a file with the line number in front of each line, use option -n. The following example, prints the line number for the lines from program.pl,
$ cat -n program.pl
1 #!/usr/bin/perl
2
3
4 if( 2 ge 3) {
5 print "greater\n";
6 } else {
7 print "lesser\n";
8 }
9
10
As you noticed above, even the empty lines are numbered. In case of numbering only nonempty lines, use option -b as follows,
$ cat -b program.pl
1 #!/usr/bin/perl

2     
3 if( 2 ge 3) {
4 print "greater\n";
5 } else {
6 print "lesser\n";
7 }
Note that the lines which contains whitespaces are not considered as empty lines and the same applicable to line numbered 2.

6. Concatenate File Contents along with Input from Stdin

There is a possibility to read lines from stdin along with concatenation of other files. Hence the user can type his own content whenever its required.
In the following example, you can insert a few lines (from stdin) in the beginning while combining files together.
$ cat - program.pl program2.pl >all_pgrm.pl
Contents from file : program.pl, program2.pl
As seen above, – is the place where you can read from stdin, accordingly 1 line from stdin has been inserted into the beginning of a new file called all_pgrm.pl with the latter contents from program.pl and program2.pl files:
$ cat -n all_pgrm.pl
1 Contents from file : program.pl, program2.pl
2 #!/usr/bin/perl
3
4
5 if( 2 ge 3) {
6 print "greater\n";
7 } else {
8 print "lesser\n";
9 }
10
11
12 #!/usr/bin/perl
13
14 @arr = qw(1 2 3);
15 $ref = \@arr;
16 print ref $ref;

7. Don’t Display Repeated Empty Output Lines

Sometimes the file would contain repeated empty lines which you don’t want to display in the stdout while listing it out. cat command provides an option called -s which will suppress consecutive empty output lines into one and displays.
As noticed in the first example of usage 5 (i.e: Display with line number infront of each lines), there is two consecutive empty output lines in the file program.pl numbered 9 and 10. May be you don’t want to display those repeated empty output lines. This can be suppressed as shown below:
# cat -sn program.pl
1 #!/usr/bin/perl
2
3
4 if( 2 ge 3) {
5 print "greater\n";
6 } else {
7 print "lesser\n";
8 }
9
Respectively the line 9 and 10 gets suppressed into one empty line in the above output (i.e:line 9).

8. Display End of Line and TAB characters

You can make the cat to display the $ character at end of every line. Normally by listing file contents, users cant identify whitespaces at the end of each lines, by using the cat -e option.
For instance, use -e option on the file program.pl. As shown below, the third line of this file (i.e:program.pl) is actually not an empty line and as well the line 7 is ending with whitespaces respectively.
$ cat -ne program.pl
1 #!/usr/bin/perl$
2 $
3     $
4 if( 2 ge 3) {$
5 print "greater\n";$
6 } else {$
7 print "lesser\n";      $
8 }$
9 $
10 $
Use option -T to display the tab characters. It displays ^I for TAB character. As shown below, line5 and line7 starts with a TAB character.
$ cat -neT program.pl
1 #!/usr/bin/perl$
2 $
3     $
4 if( 2 ge 3) {$
5 ^Iprint "greater\n";$
6 } else {$
7 ^Iprint "lesser\n";      $
8 }$
9 $
10 $

9. Read Content until a Specific Pattern

The here document can be used along with cat command. For example, when you are reading from stdin, you can read until a line that contains a specific pattern. In the example below, the block of lines are read from stdin (until EOF) and printed on the standard output.
$ cat <<EOF
> mv command to move files and directories
> top command to display linux tasks
> EOF
mv command to move files and directories
top command to display linux tasks

10. Display File Content in Reverse

This example is a cheater. This is really not a cat command example, but it is related.
tac is the reverse of cat. As you can imagine, tac will just display the contents of a file in reverse order (lines from bottom is displayed first). If you just want to reverse the characters in the line, you should use rev command.
For example, the file program.pl is being displayed in reverse as:
$ tac program.pl 

}
 print "lesser\n";
} else {
 print "greater\n";
if( 2 ge 3) {

#!/usr/bin/perl
Thanx all :)
Share this games :

1 comments:

Anonymous said...

10 Cat Command Examples To Manage Files In Linux / Unix ~ Crushhack >>>>> Download Now

>>>>> Download Full

10 Cat Command Examples To Manage Files In Linux / Unix ~ Crushhack >>>>> Download LINK

>>>>> Download Now

10 Cat Command Examples To Manage Files In Linux / Unix ~ Crushhack >>>>> Download Full

>>>>> Download LINK Gw

Post a Comment

Related Posts