Best Linux cat Command With Examples

In this article, we are going to learn how to use the Linux cat command. cat stands for Concatenate. the cat command is a file management command in Linux used to display the content of the file, create a file, edit a file, and many more. It is one of the most frequently used basic commands in Linux. Linux cat command is an open-source application released under the GNU GPLv3 license. By default, it comes as pre-installed with any Linux distribution. Here in this article, I will show you the 14 most important Linux cat command with proper example.

Best Linux Cat Command With Examples
Best Linux Cat Command With Examples

Syntax to use cat command :

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

1. Display content of a File

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

itsmarttricks@ubuntu:~$ cat test1.txt
This is a test file....

2. Display the content of multiple files

If you want to display the contents of multiple files at once you can do so using the cat command. Here I have two files i.e. text1.txt and text2.txt. So let’s check the content of these two files.

itsmarttricks@ubuntu:~$ cat test1.txt test2.txt
This is a test file....
Welcome to itsmarttricks.com

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

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

itsmarttricks@ubuntu:~$ cat /etc/login.defs | less

Also Read : Best Linux Usermod Command With Examples

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

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

itsmarttricks@ubuntu:~$ cat *.txt
This is a test file....
Ubuntu 16.04 Long Term SupportWelcome to itsmarttricks.com
Welcome to itsmarttricks.com

5. Create a new File

You can create a new file using the Linux cat command with the symbol > (Greater than). 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 the keyboard to create and save the file.

itsmarttricks@ubuntu:~$ cat > test.txt
This is a test file...

6. Dump the 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.

itsmarttricks@ubuntu:~$ cat test1.txt > myfile.txt

# Output
 
itsmarttricks@ubuntu:~$ cat myfile.txt
This is a test file....

7. Dump the content of multiple files into a new file

You can also dump the content of multiple files into a new file using the Linux cat command. Here I am dumping the content of test1.txt and test2.txt into a new file named newfile.txt.

itsmarttricks@ubuntu:~$ cat test1.txt test2.txt > newfile.txt

# Output
itsmarttricks@ubuntu:~$ cat newfile.txt 
This is a test file....
Welcome to itsmarttricks.com

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

You can append content (Write content) into an already created file using the 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 to the command below.

itsmarttricks@ubuntu:~$ cat >> test1.txt

9. Number all output Lines

You can number all output lines of any file by using the Linux cat command with argument -n. Refer to the command below.

itsmarttricks@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 Ifconfig Command With Examples

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

itsmarttricks@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

The below command will only number non-empty lines and won’t number those lines that are black or with no content.

itsmarttricks@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 to the command below.

itsmarttricks@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

The below command will show you the installed cat command package version, it’s author, and license details.

itsmarttricks@ubuntu:~$ cat --version

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

itsmarttricks@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'

Also Read : Best Useful Linux DF Command With Examples

We tried to include all possible cat command with examples. If something missed out please comment on the comment box below so that we can include that in this article.

That’s all, In this article, we have explained the cat command in Linux with examples. I hope you enjoy this article. If you like this article, then just share it. If you have any questions about this article, please comment.

Share this:
WhatsApp Channel Join Now
Telegram Channel Join Now
Instagram Channel Join Now

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.