Definition and purpose:
Cat command is used to display the content of a file or multiple files.
This command is more helpful in reading the log file or configuration files of the application or system.
Syntax:
Cat file-name1 file-name2
Example:
Cat /etc/passwd
The above command displays the user's details that are available on the machine.
The primary usage of Cat command is for displaying the file content on the terminal window but with Cat command, we can create files and add content also.
Most practical use is for:
- Checking the Application logs or system logs
- Concatenate two files or log files information and push to another file.
- Copying the content from one file to another file.
- Copying the content from one file to another file along with standard input from the terminal.
- To empty the file for having new content in it.
Below we have talked about few cat command options and their usage with practical examples.
Example-1:
Creating a new file with cat command:
Syntax:
“cat > filename”
Once the above command is typed and press enter. We see the cursor blinking on the next line, i.e, whatever we may type here, that will be redirected to that file.
Check out the below example,
[rreddy@abclearn abclearn_dir1]$ cat > abc_lab3.txt
Hi
this information saves in new file
3rd line
- must be working
this is 5th line for testing
Hi
this information saves in new file
3rd line
- must be working
this is 5th line for testing
Note:
- We have to press “ctrl+d” to come out of file writing.
- If the file is not created already then it will be created with the user credentials.
- If the file is already existing, then its content is overwritten.
- So using “>” symbol we can redirect the information to given file.
We can use the same cat command to display files content.
To display content of abc_lab3.txt file,
Cat abc_lab3.txt
We have created a new file. Now, what if we want to add more lines to that existing file.
Example-2:
Add new lines to existing file:
Syntax:
Cat >> filename
Note:
- “>” symbol is used to redirect the information into a file.
- “>>” symbol will add new text at the end of existing file.If the file doesn’t exist already, then it is created.
We can add more information to existing file at the end using “>>” symbol.
[rreddy@abclearnabclearn_dir1]$ cat >>abc_lab3.txt
this is going to be as 6th line
7th one
8th one
Let’s see the file content and confirm, it is really added.
[rreddy@abclearnabclearn_dir1]$ cat abc_lab3.txt
Hi
this information saves in new file
3rd line
- must be working
this is 5th line for testing
this is going to be as 6th line
7th one
8th one
this is going to be as 6th line
7th one
8th one
Let’s see the file content and confirm, it is really added.
[rreddy@abclearnabclearn_dir1]$ cat abc_lab3.txt
Hi
this information saves in new file
3rd line
- must be working
this is 5th line for testing
this is going to be as 6th line
7th one
8th one
Example-3:
Copy a file’s content to another file:
Syntax:
Cat source-file-name > target-file-name
As discussed earlier, “>” symbol will redirect the content which should be displayed on terminal window to another file.
So source file content is not removed at all. It is simply redirecting the file content to another file instead of on terminal.
We can use the cat command with “>” symbol to copy the content to another file and ">>" to append at the bottom of another file.
[rreddy@abclearnabclearn_dir1]$ cat abc_lab3.txt > abc_lab4.txt
[rreddy@abclearnabclearn_dir1]$ cat abc_lab4.txt
Hi
this information saves in new file
3rd line
- must be working
this is 5th line for testing this is going to be as
6th line
7th one
8th one
[rreddy@abclearnabclearn_dir1]$ cat abc_lab4.txt
Hi
this information saves in new file
3rd line
- must be working
this is 5th line for testing this is going to be as
6th line
7th one
8th one
Example-4:
Display more than one file or multiple files output on terminal:
Cat command can concatenate one or more files content and show the output on the terminal.
Syntax:
Cat file-name1 file-name2
Let’s see a practical example for better understanding.
[rreddy@abclearnabclearn_dir1]$ cat abc_lab1.txt abc_lab2.txt abc_lab3.txt
this line should replace the existing one
this is the first line in new file
Hi
this information saves in new file
3rd line
- must be working
this is 5th line for testing
this is going to be as 6th line
7th one
8th one
this line should replace the existing one
this is the first line in new file
Hi
this information saves in new file
3rd line
- must be working
this is 5th line for testing
this is going to be as 6th line
7th one
8th one
Example-5:
Redirect multiple files output to another file ( or ) concatenate more than one file output in another file:
Following the explanations are given in above examples, we will make use of “>” symbol for redirecting to file and “>>” symbol for adding the new content at the bottom of the target file.
Syntax:
Cat file-name1 file-name2 > file-name3
Cat file-name1 file-name2 >> file-name3
Cat file-name1 file-name2 >> file-name3
[rreddy@abclearnabclearn_dir1]$ cat abc_lab1.txt abc_lab2.txt abc_lab3.txt > abc_lab4.txt
[rreddy@abclearnabclearn_dir1]$ cat abc_lab4.txt
this line should replace the existing one
this is first line in new file
Hi
this information saves in new file
3rd line
- must be working
this is 5th line for testing
this is going to be as 6th line
7th one
8th one
[rreddy@abclearnabclearn_dir1]$ cat abc_lab4.txt
this line should replace the existing one
this is first line in new file
Hi
this information saves in new file
3rd line
- must be working
this is 5th line for testing
this is going to be as 6th line
7th one
8th one
Example-6:
Display the line numbers of a file on terminal:
we have a couple of options with cat command to display output with line numbering.
Syntax:
Cat -n file-name
-n will display the line numbers including the empty lines.
Observe the below practical example and see line number 12 & 14. They are empty lines.
[rreddy@abclearnabclearn_dir1]$ cat -n abc_lab4.txt
1 this line should replace the existing one
2 this is first line in new file
3 Hi
4 this information saves in new file
5 3rd line
6 - must be working
7 this is 5th line for testing
8 this is going to be as 6th line
9 7th one
10 8th one
11 I am good
12
13 How are you
14
15 closing this file here
1 this line should replace the existing one
2 this is first line in new file
3 Hi
4 this information saves in new file
5 3rd line
6 - must be working
7 this is 5th line for testing
8 this is going to be as 6th line
9 7th one
10 8th one
11 I am good
12
13 How are you
14
15 closing this file here
Example-7:
Display line numbers of a file but avoid empty lines:
For avoiding the empty lines from displaying in a file, we have command option is, “-b”
Syntax:
Cat -b file-name
Let’s look at the below cat command example, which completely avoids displaying the empty files.
[rreddy@abclearnabclearn_dir1]$ cat -b abc_lab4.txt
1 this line should replace the existing one
2 this is first line in new file
3 Hi
4 this information saves in new file
5 3rd line
6 - must be working
7 this is 5th line for testing
8 this is going to be as 6th line
9 7th one
10 8th one
11 I am good
12 How are you
13 closing this file here
1 this line should replace the existing one
2 this is first line in new file
3 Hi
4 this information saves in new file
5 3rd line
6 - must be working
7 this is 5th line for testing
8 this is going to be as 6th line
9 7th one
10 8th one
11 I am good
12 How are you
13 closing this file here
Example-8:
Display or concatenate 2 files content with terminal one and standard input:
As we have discussed earlier, cat command comes with lot of options for variety of information.
We can take files information and concatenate it with terminal input.
Syntax :
Cat filename1 – filename2
“-“ option is to support input information from terminal or standard input also.
See the below example and try it out on terminal window,
[rreddy@abclearnabclearn_dir1]$ cat abc_lab1.txt - abc_lab2.txt
this line should replace the existing one----------------------------------->abc_lab1.txt file content
hi-------------------------------------------------------------------------->information typed on terminal
hi
new lines
new lines
sadfa
sadfa
this is first line in new file----------------------------------------------->abc_lab2.txt file content
this line should replace the existing one----------------------------------->abc_lab1.txt file content
hi-------------------------------------------------------------------------->information typed on terminal
hi
new lines
new lines
sadfa
sadfa
this is first line in new file----------------------------------------------->abc_lab2.txt file content
Example-9:
Emptying the file having the content:
In such cases we need to empty the files to reduce the utilization of the storage in the machine.
Syntax:
Cat /dev/null > File-name
[rreddy@abclearnabclearn_dir1]$ cat /dev/null > abc_lab2.txt [rreddy@abclearnabclearn_dir1]$ cat abc_lab2.txt
Exploring more cat command options:
For more on command options and their understanding, check out the manual pages or help content given with cat command.
Syntax:
man cat
cat --help
info cat
cat --help
info cat
0 comments:
Post a Comment