Best Linux Cat command with examples

This article explains how to use the Linux cat command. Cat stands for concatenate, The cat command is a file management command in Linux that displays the contents of the file, creates a file, edits a file, and more. It is one of the most commonly used basic commands in Linux. Linux cat command is an open source application published under the GNU GPLv3 license. By default, it is preinstalled in every Linux distribution. Here in this article I will show you the 14 most important Linux command cat using a suitable example.

Best Linux Cat command with examples

Syntax for using the cat command:

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

1. View the contents of a file

You can view the contents of a file using the Linux cat command. See the following command.

[email protected]ubuntu:~$ cat test1.txt
This is a test file....

2. View the contents of multiple files

If you want to display the contents of several files at the same time, you can do this with the cat command. Here I have two files, i.e. text1.txt and Text2.txt, So let’s check the contents of these two files.

[email protected]:~$ cat test1.txt test2.txt
This is a test file....
Welcome to itsmarttricks.com

3. How do I use the Linux cat command with less / more command with pipe (|)

If you want to display a long file that does not appear on a single screen, use the cat command with the command command fewer with the help of pipe (|). See the following command.

[email protected]:~$ cat /etc/login.defs | less

Read also: Best Linux Usermod Command with Examples

4. View the contents of all files with the same extension at the same time

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

[email protected]:~$ 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 icon > (Greater than). after executing the command (cat> test.txt) You have to enter a content that you want to save in this file. Enter a text and then press CTRL + D on the keyboard to create and save the file.

[email protected]:~$ cat > test.txt
This is a test file...

6. Copy the contents of one file to another file

Suppose you have a file with some content and want to copy all of the content to a new file. You can use the cat command with the symbol to do this > (Greater than). Here I have a file called test1.txt with some content and I want to output all of the content in a new file named myfile.txt,

[email protected]:~$ cat test1.txt > myfile.txt

# Output
 
[email protected]:~$ cat myfile.txt
This is a test file....

7. Save the contents of multiple files in a new file

You can also save the contents of multiple files to a new file using the Linux cat command. Here I empty the contents of test1.txt and test2.txt into a new file called newfile.txt,

[email protected]:~$ cat test1.txt test2.txt > newfile.txt

# Output
[email protected]:~$ cat newfile.txt 
This is a test file....
Welcome to itsmarttricks.com

8. Attaching content to an already created file (editing a file)

With the command cat with the symbol you can attach content to an already created file (write content) >> (Double greater than). After running the following command, you need to enter the content you want to save in this file and then press CTRL + D on the keyboard to save and close the file. See the following command.

[email protected]:~$ cat >> test1.txt

9. Number all output lines

You can number all output lines of any file using the Linux cat command with an argument -n, See the following command.

[email protected]:~$ 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

Read also: Best Linux Ifconfig command with examples

10. Display the end of each line

cat command with argument -E will place one $ Characters at the end of each output line. This command is useful if you want to specify the end of each output line. Refer to the command with the following example.

[email protected]:~$ 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. Only number non-blank output lines

The following command only numbers non-blank lines and does not number lines that are black or empty.

[email protected]:~$ 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 "https://www.itsmarttricks.com/#") 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. Show tab characters

You can use the Linux cat command with an argument -T to display tab characters. All lines that start with tabs are displayed as ^ I, See the following command.

[email protected]:~$ 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 the installed cat command package

The following command shows you the installed version of the cat command package, the author and the license details.

[email protected]:~$ cat --version

For more help using this command with all of the available arguments, see the following command.

[email protected]:~$ 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: 
Full documentation at: 
or available locally via: info '(coreutils) cat invocation'

Also Read: Best Useful Linux DF Command With Examples

We tried to give examples of all possible cat commands. If something is missed, please comment in the comment box below so we can include this in this article.

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

Sidebar