14 Useful “cat” Command Examples in Linux

Intoduction

When learning Unix, it’s common to first learn the cat command. An easy abbreviation to remember if you are pet-friendly, this command will concatenate the elements you specify into one package. The easiest way to look at files is to use cat, the concatenate command.

In this article, we are going to learn how to use the Linux cat command. As discussed earlier, 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 an open source application released under GNU GPLv3 license. By default, it comes pre-installed with any Linux distribution. Here in this article, I will show you 14 most important Linux cat command with examples.

Syntax to use cat command is:

# cat [OPTION]... [FILE]...

1. Display content of a File

You can display content of a file using Linux cat command. Refer the command below.

$ 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 the content of these two files.

$ cat test1.txt test2.txt
This is a test file...
This is another test file...

3. How to use Linux cat command with less/more command with Pipe (|)

If you want to display a long file which can not be displayed under single screen then use cat command with command less with the help of a pipe (|). Refer the command below.

$ cat /etc/login.defs | less

4. Display content of all files at once with same extension

Suppose you have many files with the same extension. Let’s say I have a number of text files whose extension is *.txt. Now display the content of these files using Linux cat command as shown below.

$ cat *.txt
This is a test file....
Ubuntu 16.04 Long Term Support
Welcome to thegeekdiary.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.

$ cat > test.txt
This is a test file...
ctrl+d

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 Than). 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.

$ cat test1.txt > myfile.txt

# Output
 
$ 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 into 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.

$ cat test1.txt test2.txt > newfile.txt

# Output

$ cat newfile.txt 
This is a test file....
Welcome to thegeekdiary.com

8. Append content into an already created file (Edit a File)

You can append content (Write content) into an already created file using cat command with symbol >> (Double Greater than). 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 the keyboard to save and close the file. Refer the command below.

$ 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.

$ 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

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 verify end of each output line. Refer the command with example below.

$ cat -E /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$
adm:x:3:4:adm:/var/adm:/sbin/nologin$
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin$
sync:x:5:0:sync:/sbin:/bin/sync$
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown$
halt:x:7:0:halt:/sbin:/sbin/halt$
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin$
operator:x:11:0:operator:/root:/sbin/nologin$
games:x:12:100:games:/usr/games:/sbin/nologin$
ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin$
nobody:x:99:99:Nobody:/:/sbin/nologin$
dbus:x:81:81:System message bus:/:/sbin/nologin$
...

11. Number only non empty output Lines

Below command will only number non-empty lines and not number those lines which are blank.

$ 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 containing “tab” would be indicated as ^I. Refer the command below.

$ 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.

$ cat --version

For more help on usage of this command with all available arguments refer the below command.

$ 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'

I hope we have included all the important and most commonly used cat command examples. Being one of the basic and most widely used commands, it is important to know some advanced features and options available with the command. I hope the post was helpful.

Related Post