Tuesday, January 8, 2019

Cheat sheet for cat command


Display content of the file
cat /etc/passwd

View contents of the multiple files in the terminal
cat sample1 sample2
First sample
This is the second sample;

Create file with cat ctrl+d to exit
cat >testcat
Testing file with cat command



Cat Command with More & Less Options
If file having large number of content that won’t fit in output terminal and screen scrolls up very fast, we can use parameters more and less with cat command as show above.
cat cat_more | more

Display Line Numbers in File With -n option 
you could see the line numbers of a file cat_more in the output terminal
cat -n cat_more
 Output

Display $ at the End of File
In the below, you can see with -e option that ‘$‘ is shows at the end of line and also in space showing ‘$‘ if there is any gap between paragraphs. This options is useful to squeeze multiple lines in a single line.
cat -e cat_more
output

Display Tab separated Lines in File
In the below output, we could see TAB space is filled up with ‘^I‘ character

cat -t tab_file.txt
First Line^I
Second Line ^I

Using Greater than > symbol
We can redirect standard output of a file into a new file else existing file with ‘>‘ (greater than) symbol. Careful, existing contents of test1 will be overwritten by contents of test file.
 cat -t tab_file.txt > new_file.txt
[vn04nxr@client-236651385-1-270480917 ~]$ cat new_file.txt
First Line^I
Second Line ^I

Using redirection symbol
Appending Standard Output with Redirection Operator Appends in existing file with ‘>>‘ (double greater than) symbol. Here, contents of test file will be appended at the end of test1 file.

cat -t tab_file.txt >> new_file.txt
[vn04nxr@client-236651385-1-270480917 ~]$ cat new_file.txt
First Line^I
Second Line ^I
First Line^I
Second Line ^I
Multiple file content into a single file
We can create single file from multiple files as shown below
[vn04nxr@client-236651385-1-270480917 ~]$ cat > first_file
first file
[vn04nxr@client-236651385-1-270480917 ~]$ cat > sec_file
second file
[vn04nxr@client-236651385-1-270480917 ~]$ cat > third_file
third file
[vn04nxr@client-236651385-1-270480917 ~]$  cat first_file sec_file third_file > final_file
[vn04nxr@client-236651385-1-270480917 ~]$ cat final_file
first file
second file
third file

No comments:

Post a Comment