Monday, 11 November 2013

cat : Basic File Operation

cat Command:

cat command is basically used for file oriented operations like:
·         Open a file
·         Create a file
·         Write to a file
·         Modify a file
·         Append to a file
·         Combine files
There are also some other operations where cat command involves:
·         Creating Zero byte(NULL) file
·         Counting lines in a file
·         Displaying Non printing ASCII characters

There are some redirection operators which is used with cat (as well as other command too), these are:
<       input redirection operators
>       output redirection operators
>>     append redirection operators
<<     here document
>$      associate operator   



Opening a file in READ ONLY manner:
cat <file_name>
e.g. cat F.txt
if F.txt is a file name.
Opening multiple files in READ ONLY manner:
cat A.txt B.txt C.txt
Here 3 files will be open in a sequence as the names are given in command, this way you can use this command to open many number files as you wish.

Creating a new file:
cat > F.txt
This is a test.
Unix is the best.
[ctrl+d]


Writing into a file which is created before / modifying the contents into a file:

cat > F.txt
This is a test.
Unix is the best.
[ctrl+d]


Append to a file:

cat >> F.txt
This is a test.
Unix is the best.
You should learn UNIX

UNIX and linux has same flavor.

[ctrl+d]


Combining two/multiple files:

Appending contents of one file to another file:

Cat F.txt >> G.txt

After this command when you open file G.txt you will see contents of F.txt after the contents of G.txt.  

Redirecting/ Overrating contents of one file to another file:

cat F.txt > G.txt

After this command when you open file G.txt you will see only the contents of F.txt is remaining and contents of file G.txt is vanished i.e. contents of file F.txt is overwritten into G.txt.

Redirecting contents of multiple file into one file/ Combining multiple files:

cat A.txt B.txt C.txt > F.txt

Appending contents of multiple file into one file/ Combining multiple files:

cat A.txt B.txt C.txt >> F.txt





cat with options/ other uses of cat command:

Creating Zero byte(NULL) file:
cat F.txt > F.txt
Keep the same file both side of output redirection operator.

Counting lines in a file
cat –n F.txt
This will count total number of line including the blank lines in a file F.txt. Line numbers will be displayed at the beginning of every line.
cat –b F.txt
This will count total number of line excluding the blank lines in a file F.txt. Line numbers will be displayed at the beginning of every line.

Displaying Non printing ASCII characters
cat –v F.txt
Non-printing characters (with the exception of tabs, new-lines and form-feeds) are printed visibly. ASCII control characters (octal 000 - 037) are printed as ^n, where n is the corresponding ASCII character in the range octal 100 - 137 (@, A, B, C, . . ., X, Y, Z, [, \, ], ^, and _); the DEL character (octal 0177) is printed ^?. Other non-printable characters are printed as M-x, where x is the ASCII character specified by the low order seven bits.

Display $ at the end of every line
cat –e F.txt
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.

 

Find out TAB characters

cat –T F.txt
Use option -t to display the tab characters. It displays ^I for TAB character.


Use Cat Command with More & Less Options

cat F.txt | more
cat F.txt | less
If file having huge contents that will not fit in output terminal and screen scrolls up very fast and you are not able to see whole content at once, we can use parameters more and less with cat command as show above. This will show you file contents page by page while pressing spacebar.











No comments:

Post a Comment