Thursday, August 26, 2010

cat in linux


  1. Read/scan the man page for cat with the command:
         man cat 
  2. Use this command to display the contents of a file. What happens?
         cat  filename 
  3. Now try this command notice the difference. How many lines are in the file?
         cat -n  filename 
  4. The cat command is more often used for purposes other than just displaying a file. Try these commands to "concatenate" two files into a new, third file:
         cat file1                  - first, show file1 
         cat file2                  - then, show file2 
         cat file1 file2 > newfile  - now do the actual concatenate 
         cat newfile                - finally, show the result 

-A Show all.
-b Omits line numbers for blank space in the output.
-e A $ character will be printed at the end of each line prior to a new line.
-E Displays a $ (dollar sign) at the end of each line.
-n Line numbers for all the output lines.
-s If the output has multiple empty lines it replaces it with one empty line.
-T Displays the tab characters in the output.
-v Non-printing characters (with the exception of tabs, new-lines and form-feeds) are printed visibly. 


  1. To Create a new file:
    cat > file1.txt
    This command creates a new file file1.txt. After typing into the file press control+d (^d) simultaneously to end the file.
  2. To Append data into the file:
    cat >> file1.txt
    To append data into the same file use append operator >> to write into the file, else the file will be overwritten (i.e., all of its contents will be erased).
  3. To display a file:
    cat file1.txt
    This command displays the data in the file.
  4. To concatenate several files and display:
    cat file1.txt file2.txt
    The above cat command will concatenate the two files (file1.txt and file2.txt) and it will display the output in the screen. Some times the output may not fit the monitor screen. In such situation you can print those files in a new file or display the file using less command.
    cat file1.txt file2.txt | less
  5. To concatenate several files and to transfer the output to another file.
    cat file1.txt file2.txt > file3.txt
    In the above example the output is redirected to new file file3.txt. The cat command will create new file file3.txt and store the concatenated output into file3.txt.



Post a Comment