Linux CAT command

Introduction

CAT Command

'cat' command concatenate files to the Standard Output. The name of this command is derived from 'concatenate' function it performes. It is very popular in Linux/Unix Operating Systems to read input stream/file and prints to Standard output stream. Cat comes with really helpful options/switches to get the desired output.

Checkout video below for CAT command's basic usage with examples:




CAT Options and usage

  1. Create a new file
  2. Creating a new file using CAT is simple.

    Let's say we want to create a new file with name foo.txt.

    Just use below command in terminal.

    cat > foo.txt

    Above command uses '>'(greater than) symbol to tell the system that we are going to create new file.

    Cat will ask for the input content of the files. Write content for the file and press Ctrl+d. foo.txt will be created with the content provided.

    Note : If you don't provide the content of the file CAT will create an empty file.

  3. Display a file content
  4. To read a file use below command:

    cat <filename>

    In above screenshot, we have copied the content of foo.txt to bar.txt file.

    Note : If bar.txt doesn't exist then CAT will create it.

    Check the file content

  5. Copy content of one file to another file
  6. To copy content of a file to another file, use below command:

    cat <sourceFileName> > <destFileName>

    Above copy command overwrites the content of destination file. To append the content use below command:

    cat <sourceFileName> >> <destFileName>

    Note the two >> symbols continuously in above command. If we run the above command and check the destination file, we will get the content of foo.txt as well as bar.txt.

  7. Display content of Multiple files
  8. To display content of Multiple files, use below command:

    cat <file1> <file2> ... so on

    This command will merge the content of all the files and will display it in the terminal in the sequence of filenames provided in the command.

  9. Display content with line numbers
  10. Sometimes we also want to see the content of file with the line numbers.

    CAT provides -n switch for this. Command will be as follows:

    cat -n <filename>

    To display content of Multiple files, use below command:

    cat <file1> <file2> ... so on

    This command will merge the content of all the files and will display it in the terminal in the sequence of filenames provided in the command.

  11. Display content with line numbers
  12. Sometimes we also want to see the content of file with the line numbers.

    CAT provides -n switch for this. Command will be as follows:

    cat -n <filename>
  13. Display content of file with non-empty line numbers
  14. We can skip the line numbering for the empty lines.

    CAT provides -b switch for this. Command will be as follows:

    cat -b <filename>
  15. Display file content and suppress repeated empty output lines
  16. By default, CAT will display all the content as is. But if file contains multiple repeated empty lines it will not be an easy task to search something from the output.

    We can suppress the repeated empty lines to not to include these in the output of the CAT.

    CAT provides -s switch for this. Command will be as follows:

    cat -s <filename>

    Notice the difference in the below image between without suppress switch and with suppress switch

  17. Display file content with Special characters
  18. Special characters like tab can be displayed using -T switch.

    Command will be:

    cat -T <filename>

    ^I characters in above image representing the tab space in the file's content.

    To display End of Line in characters in the file use -E switch.

    cat -E <filename>

    $ characters in above image representing the end of line in the file's content.

Conclusion

We have seen the basic usage of the CAT command in Linux system which are useful in reading and writing files. Try these yourself and let me know your thoughts in the comment section.

Comments

Popular posts from this blog

How to Detect CapsLock key using Java in NetBeansIDE.

How to Perform DES Encryption and Decryption of TXT file in JAVA with NetBeansIDE

How to insert Image in MySQL Database with Java in NetBeansIDE