Learn Cat Commands With Examples On Linux

Learn Cat Commands With Examples On CentOS 8

We can use cat commands to see the contents of a filename. We will show you the cat commands example in Linux. Let’s learn cat commands on Linux. You can also run cat command in Linux as well as Unix.

Check Syntax:

Usage: cat [OPTION]... [FILE]...

Check Out: How To Install Deluge Bit Torrent Client on Ubuntu 20.04

1. Display the contents of files: 

We will show you the contents of a file using the below commands. Bas h cat in  c more command

[root@localhost ~]# cat /etc/passwd
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin

2. Create a file using the command:

We can also use this command to create a file like a touch command. 

[root@localhost ~]# cat >test
hello world, where are you?

When you’re creating a file using the above command, it will wait for the message to write into the file as I have written in the example. Once you’re done with the message just press ctlrl+d. it will leave the prompt. Now you can see the same content will be present in the file.

[root@localhost ~]# cat test
hello world, where are you?

Check Out: Useful Git Command For Admin and Developers On Linux

3. Sort content of multiple files into a single file: 

We have created new three files with messages. Test2 has the message “abc” and test3 has the message “hello“. we have to sort all files into test4.

[root@localhost ~]# cat test test2 test3 | sort > test4
[root@localhost ~]# cat test4
hello world, where are you?abchello

4. See output with more and less command: 

You can also check the output of a large number of contents with more and less command along with cat command.

[root@localhost ~]# cat test4 | more
[root@localhost ~]# cat test4 | less

5. Append output of one file into another file: 

We can also use the below command to print the output of one file to other files. No need to create a test6 file it will create automatically.

[root@localhost ~]# cat test4 >> test6
[root@localhost ~]# cat test6
hello world, where are you?abchello

Check Out: How To Upgrade Linux Kernel Version On CentOS

6. Display the content of multiple files in a single command: 

We can also use this command to display the contents of multiple files.

[root@localhost ~]# cat test2 test3
abc
hello

7. Display tab used in the files: 

If you want to know where the tab is used in the file, we can find it using ‘-T‘ options. we have created a file where we have used tab to keep distance between the words. The ‘^I‘ indicates the tab used.

[root@localhost ~]# cat -T test2
abc^Ihellowworld^Iwe're using tab
^Ican you help?

8. Display line numbers:

You can use ‘-n’ to display the line number in the output of files.

[root@localhost ~]# cat -n test2
1 abc 
2 hellowworld 
3 we're using tab
4 can you help?

Check Out: Learn Useful Tar Command With Example In Linux

9. Check the Version installed:

Use the below command to check the version of the cat command.

[root@localhost ~]# cat --version  
cat (GNU coreutils) 8.30
Copyright (C) 2018 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <https://gnu.org/licenses/gpl.html>.
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.

Written by Torbjorn Granlund and Richard M. Stallman.

10. Display the ‘$’ at the end of lines: 

If you want to display the ‘$’ at the end of lines in a file, we can either use ‘-E‘ or ‘-e‘ options.

[root@localhost ~]# cat -E test2
abc $
hellowworld $
we're using tab $
can you help? $

11. Display the content in reverse order: 

The below command displays the contents of a file in reverse order.

[root@localhost ~]# tac test2
can you help?
we're using tab
hellowworld 
abc

Check Out: Copy Data Using Rsync And SCP Command On Linux 7

12. if you want to use these options together ‘-v‘, ‘-E‘, and ‘-T‘ to display the contents. 

[root@localhost ~]# cat -EvT test2
abc^I$
hellowworld^I$
we're using tab$
can you help?$

Then you can use the ‘-A‘ that works the same as shown in the above commands.

[root@localhost ~]# cat -A test2
abc^I$
hellowworld^I$
we're using tab$
can you help?$

13. Suppose you want to see the contents of a folder with a particular file type pattern. we have created a few files and will display the contents. 

[root@localhost ~]# mkdir file
[root@localhost ~]# cd file/
[root@localhost file]# echo "testing file" > file1.txt
[root@localhost file]# echo "testing file 2" > file2.txt
[root@localhost file]# echo "testing file 3" > file3.txt
[root@localhost file]# cat *.txt
testing file
testing file 2
testing file 3

You’re done cat filename

Share on:

I'm the founder of Curious Viral. I hope this blog will provide you complete information about Linux Technology & I would like to share my technical knowledge with you which I have learned during this period.

Other Posts You May Like...

Leave a comment