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
- Create a new file
- Display a file content
- Copy content of one file to another file
- Display content of Multiple files
- Display content with line numbers
- Display content with line numbers
- Display content of file with non-empty line numbers
- Display file content and suppress repeated empty output lines
- Display file content with Special characters
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.
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
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.
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.
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.
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>
We can skip the line numbering for the empty lines.
CAT provides -b switch for this. Command will be as follows:
cat -b <filename>
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
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
Post a Comment