08 October 2011

cat command in Linux/Unix

Leave a Comment
cat command in Linux/Unix


cat command is one of the command which is used frequently in UNIX/Linux. cat command is used mainly for :-


1. To display content of text file on screen.


2. To concatenate text files .


3. To copy the content of text file.


4. To create new text file.


Syntax :


1 . cat xyz.lst       -- display content of file xyz.lst


2.  cat xyz.lst  abc.lst   -- concatenate the content of two files and display on screen


3.  cat xyz.lst > def.lst   --- copy the content of xyz.lst to def.lst


4.  cat > xyz.lst    -- create new file xyz.lst.


let we have two files abc.lst and xyz.lst


content of abc.lst


my name is xyz
iam a good boy


content of xyz.lst


my name is abc
iam also good boy


now execute commands one by one and see what happen..


1.  cat  abc.lst


output: 
$ cat abc.lst
my name is xyz
iam a good boy
$


2. cat abc.lst xyz.lst


output:
$ cat abc.lst xyz.lst
my name is xyz
iam a good boy
my name is abc
iam also a good boy
$

3. cat abc.lst > def.lst   ---create new file def.lst , copy the content of abc.lst to def.lst . This time no output visible on screen .


To see content of def.lst


-->cat def.lst


output:
$ cat abc.lst > def.lst
$ cat def.lst
my name is xyz
iam a good boy
$


4. cat > pqr.lst [press enter and write what ever you want]
my name is pqr 
iam also a good  person
^C


*press ctrl+c to end file


To see content of pqr.lst


-->cat pqr.lst


output:
$ cat > pqr.lst
my name is pqr
iam also a good person
^C
$ cat pqr.lst
my name is pqr
iam also a good person
$

*cat is very useful when it is used with input/output redirection operators like | ,>>,<,>. This will be discussed later.


Options used with cat command:


let we have file cut1 which contain following lines :

$ cat > cut1
sandeep

vaibhav
anurag
ankit
parth
udit
panwar
^C
$


1. -n --  used to number all lines


       cat -n cut1


output:
$ cat -n cut1
1 sandeep
2
3 vaibhav
4 anurag
5 ankit
6 parth
7 udit
8 panwar
$


     


1. -b --  used to number only non-blank lines.


cat -b cut1


output:
$ cat -b cut1
1 sandeep

2 vaibhav
3 anurag
4 ankit
5 parth
6 udit
7 panwar
$




If You Liked This Post Please Take a Time To Share This Post

You May Also Like...

0 comments:

Post a Comment