14 BEST LINUX CAT COMMAND WITH EXAMPLES
Introduction
In this article we are going to learn how to use Linux cat command. cat stands for Concatenate. cat command is a file management command in Linux used to display content of the file, create a file, edit file and many more. It is one of the most frequently used basic command in Linux. Linux cat command is a open source application released under GNU GPLv3 license. By default it’s comes as pre installed with any Linux distribution. Here in this article I will show you 14 most important Linux cat command with proper example.
Syntax to use cat command :
cat [OPTION]... [FILE]...
1. Display content of a File
You can display content of a file using Linux cat command. Refer the command below.
elinuxbook@ubuntu:~$ cat test1.txt This is a test file....
2. Display content of multiple files
If you want to display contents of multiple files at once you can do so using cat command. Here I have two files i.e. text1.txt and text2.txt. So let’s check content of these two files.
elinuxbook@ubuntu:~$ cat test1.txt test2.txt This is a test file.... Welcome to elinuxbook.com
3. How to use Linux cat command with less/more command with Pipe (|)
If you want to display a long file which is not comes under single screen then use cat command with command less with the help of pipe (|). Refer the command below.
elinuxbook@ubuntu:~$ cat /etc/login.defs | less
4. Display content of all files at once with same extension
Suppose I have so many files with same extension. Let’s say I have so many text files whose extension is *.txt. Now display the content of these files using Linux cat command.
elinuxbook@ubuntu:~$ cat *.txt This is a test file.... Ubuntu 16.04 Long Term SupportWelcome to elinuxbook.com Welcome to elinuxbook.com
5. Create a new File
You can create a new file using Linux cat command with symbol > (Greater Then). after running the command (cat > test.txt) you have to enter some content you want to store in that file. So type some text and then press CTRL+D on keyboard to create and save the file.
elinuxbook@ubuntu:~$ cat > test.txt This is a test file...
6. Dump content of one file to another file
Suppose you have a file with some content and you want to copy all that content to a new file. You can do so using cat command with symbol > (Greater Then). Here I have a file named test1.txt with some content and I want to dump all that content to a new file named myfile.txt.
elinuxbook@ubuntu:~$ cat test1.txt > myfile.txt
# Output
elinuxbook@ubuntu:~$ cat myfile.txt
This is a test file....
7. Dump content of multiple files in to a new file
You can also dump content of multiple files in to a new file using Linux cat command. Here I am dumping the content of test1.txt and test2.txt in to new file named newfile.txt.
elinuxbook@ubuntu:~$ cat test1.txt test2.txt > newfile.txt
# Output
elinuxbook@ubuntu:~$ cat newfile.txt
This is a test file....
Welcome to elinuxbook.com
8. Append content in to a already created file (Edit a File)
You can append content (Write content) in to a already created file using cat command with symbol >> (Double Greater Then). After running the below command you have to type the content that you want to store in that file and then press CTRL+D on keyboard to save and close the file. Refer the command below.
elinuxbook@ubuntu:~$ cat >> test1.txt
9. Number all output Lines
You can number all output lines of any file by using Linux cat command with argument -n. Refer the command below.
elinuxbook@ubuntu:~$ cat -n /etc/passwd 1 root:x:0:0:root:/root:/bin/bash 2 daemon:x:1:1:daemon:/usr/sbin:/usr/sbin/nologin 3 bin:x:2:2:bin:/bin:/usr/sbin/nologin 4 sys:x:3:3:sys:/dev:/usr/sbin/nologin 5 sync:x:4:65534:sync:/bin:/bin/sync 6 games:x:5:60:games:/usr/games:/usr/sbin/nologin 7 man:x:6:12:man:/var/cache/man:/usr/sbin/nologin 8 lp:x:7:7:lp:/var/spool/lpd:/usr/sbin/nologin 9 mail:x:8:8:mail:/var/mail:/usr/sbin/nologin 10 news:x:9:9:news:/var/spool/news:/usr/sbin/nologin
Also Read :
- BEST LINUX USERMOD COMMAND WITH EXAMPLES
- BEST LINUX SHUTDOWN COMMAND WITH EXAMPLES
- BEST LINUX CHAGE COMMAND WITH EXAMPLES – A LINUX PASSWORD EXPIRATION MANAGEMENT TOOL
10. Show end of each Line
cat command with argument -E will place a $ sign at the end of each output line. This command is useful if you want to indicate end of each output line. Refer the command with example below.
elinuxbook@ubuntu:~$ cat -E /etc/passwd root:x:0:0:root:/root:/bin/bash$ daemon:x:1:1:daemon:/usr/sbin:/usr/sbin/nologin$ bin:x:2:2:bin:/bin:/usr/sbin/nologin$ sys:x:3:3:sys:/dev:/usr/sbin/nologin$ sync:x:4:65534:sync:/bin:/bin/sync$ games:x:5:60:games:/usr/games:/usr/sbin/nologin$ man:x:6:12:man:/var/cache/man:/usr/sbin/nologin$ lp:x:7:7:lp:/var/spool/lpd:/usr/sbin/nologin$ mail:x:8:8:mail:/var/mail:/usr/sbin/nologin$ news:x:9:9:news:/var/spool/news:/usr/sbin/nologin$ uucp:x:10:10:uucp:/var/spool/uucp:/usr/sbin/nologin$
11. Number only non empty output Lines
Below command will only number non-empty lines and won’t number those lines which are black or with no content.
elinuxbook@ubuntu:~$ cat -b /etc/login.defs 1 # 2 # /etc/login.defs - Configuration control definitions for the login package. 3 # 4 # Three items must be defined: MAIL_DIR, ENV_SUPATH, and ENV_PATH. 5 # If unspecified, some arbitrary (and possibly incorrect) value will 6 # be assumed. All other items are optional - if not specified then 7 # the described action or option will be inhibited. 8 # 9 # Comment lines (lines beginning with "#") and blank lines are ignored. 10 # 11 # Modified for Linux. --marekm 12 # REQUIRED for useradd/userdel/usermod 13 # Directory where mailboxes reside, _or_ name of file, relative to the 14 # home directory. If you _do_ define MAIL_DIR and MAIL_FILE, 15 # MAIL_DIR takes precedence.
12. Display Tab characters
You can use the Linux cat command with argument -T to display Tab characters. All lines started it Tab characters would be indicated as ^I. Refer the command below.
elinuxbook@ubuntu:~$ cat -T /etc/login.defs
################# OBSOLETED BY PAM ##############
#^I^I^I^I^I^I#
# These options are now handled by PAM. Please^I#
# edit the appropriate file in /etc/pam.d/ to^I#
# enable the equivelants of them.
#
###############
################# OBSOLETED #######################
#^I^I^I^I^I^I #
# These options are no more handled by shadow. #
# #
# Shadow utilities will display a warning if they #
# still appear. #
# #
###################################################
# CLOSE_SESSIONS
# LOGIN_STRING
# NO_PASSWORD_CONSOLE
# QMAIL_DIR
13. Check the version of cat command installed package
Below command will show you the installed cat command package version, it’s author and license details.
elinuxbook@ubuntu:~$ cat --version
For more help on usage of this command with all available arguments refer the below command.
elinuxbook@ubuntu:~$ cat --help Usage: cat [OPTION]... [FILE]... Concatenate FILE(s) to standard output. With no FILE, or when FILE is -, read standard input. -A, --show-all equivalent to -vET -b, --number-nonblank number nonempty output lines, overrides -n -e equivalent to -vE -E, --show-ends display $ at end of each line -n, --number number all output lines -s, --squeeze-blank suppress repeated empty output lines -t equivalent to -vT -T, --show-tabs display TAB characters as ^I -u (ignored) -v, --show-nonprinting use ^ and M- notation, except for LFD and TAB --help display this help and exit --version output version information and exit Examples: cat f - g Output f's contents, then standard input, then g's contents. cat Copy standard input to standard output. GNU coreutils online help: <http://www.gnu.org/software/coreutils/> Full documentation at: <http://www.gnu.org/software/coreutils/cat> or available locally via: info '(coreutils) cat invocation'
We tried to include all possible Linux cat command with examples. If some thing missed out please comment on comment box below so that we can include that in this article.
If you found this article useful then Like us, Share this post on your preferred Social media, Subscribe our Newsletter OR if you have something to say then feel free to comment on the comment box below.