QUICK TIP - Useful Cat Command in Linux with 7 easy Examples

0 65,623
linux_administration_complete_bootcamp_2018.jpg | A&H Business Technology
Related posts

Cat Command in Linux with 7 simple and easy Examples – The cat command (short for “concatenate “) is one of the most frequently used commands in Linux/Unix, Apple Mac OS X operating systems.

Cat command allows us to create single or multiple files, view contain of file, concatenate files and redirect output in terminal or files. It is a standard Unix program used to concatenate and display files.

The cat command display file contents to a screen. Cat Command in Linux concatenate FILE(s), or standard input, to standard output. With no FILE, or when FILE is -, it reads standard input.

Also, you can use Cat Command in Linux for quickly creating a file. The Cat Command in Linux can read and write data from standard input and output devices.

It has three main functions related to manipulating text files: creating them, displaying them, and combining them.


The Cat Command in Linux is used for:

  1. Display text file on screen
  2. Create a new text file
  3. Read text file
  4. Modifying file
  5. File concatenation

The basic syntax of cat command is as follows:

$ cat filename


$ cat > filename


$ cat [options] filename

Options-Cat Command in Linux

These options are available on GNU cat, which is standard on most Linux distributions.

If you are using a different Unix-like operating system (BSD, for example), some of these options may not be available; check your specific documentation for details.

-A–show-all Equivalent to -vET.
-b–number-nonblank Number non-empty output lines. This option 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.
-v–show-nonprinting Use ^ and M- notation, except for LFD and TAB.
–help Display a help message, and exit.
–version Output version information, and exit.

Cat Command Examples

1. Displaying text files

To view a file using Cat Command in Linux, you can use the following command.

$ cat filename

The simplest way to use cat is to give it the name of a text file. It displays the contents of the text file on the screen. For instance:

…will read the contents of mytext.txt and send them to standard output (your terminal screen).

If mytext.txt is very long, they will zoom past and you will only see the last screen’s worth of your document.

If you want to view the document page-by-page or scroll back and forth through the document, you can use a pager or viewer such as pgmore, or less.

If you specify more than one file name, cat displays those files one after the other, catenating their contents to standard output. So this command:

Will print the contents of those two text files as if they were a single file.


2. Create a new file

You can create a new file with the name file1.txt using the following Cat Command in Linux and you can type the text you want to insert in the file.

Make sure you type ‘Ctrl-d’ at the end to save the file.

$ cat > file1.txt

This is my new file using Cat Command in Linux.

The Cat Command in Linux is very useful.


After pressing Enter, you are not returned to the terminal prompt. Instead, the cursor is placed on the next line, and you can start entering text directly into your file.

Type your lines of text, pressing Enter after each line. When you are done, press Ctrl+D to exit the file and return to the prompt.

Now you can display the contents of the file file1.txt by using the following command.

$ cat file1.txt

This is my new file in Linux.

The cat command is very useful.



3. Concatenate sources to create a new text file

To create two sample files and you need to concatenate them, use the following command.

$ cat smaple1.txt

This is my first sample text file

$ cat sample2.txt

This is my second sample text file

Now you can concatenate these two files and can save to another file named sample3.txt. For this, use the below given command.

$ cat sample1.txt sample2.txt > sample3.txt

$ cat sample3.txt

This is my first sample text file

This is my second sample text file

4. Display all content of a text file

To display contents of all txt files, use the following command.

$ cat *.txt

This is my first sample text file

This is my second sample text file

5. Display Line Numbers in File

To display the contents of a file with line number, use the following command. With -n option you could see the line numbers of a file file1.txt in the output terminal.

$ cat -n file1.txt

1  This is my new file in Linux.

2  The cat command is very useful.

3  Thanks

6. Copy a text file

Normally you would copy a file with the cp command. You can use Cat Command in Linux to make copies of text files in much the same way.

cat sends its output to stdout (standard output), which is usually the terminal screen. However, you can redirect this output to a file using the shell redirection symbol “>“.

For instance, this command:

To copy the content of one file to another file, you can use the greater than ‘>’ symbol with the cat command.

$ cat file2.txt> file1.txt

7. Append a text file’s contents to another text file

Instead of overwriting another file, you can also append a source text file to another using the redirection operator “>>“.

For instance: To append the contents of mytext.txt to another-text-file.txt, you can use the double greater than ‘>>’ symbol with the Cat Command in Linux.

$ cat mytext.txt >> another-text-file.txt

The syntax above will read the contents of mytext.txt, and write them at the end of another-text-file.txt.

If another-text-file.txt does not already exist, it will be created and the contents of mytext.txt will be written to the new file.

The cat command also works for multiple text files as well:

…will write the combined contents of mytext.txt and mytext2.txt to the end of another-text-file.txt.

Final words

In order to remember Cat Command in Linux, we create a summary of the cat command syntax and its functions as examples above as an image below.

Cat Command in Linux

If you need any further assistance please contact our support.

TinyURL for this post: https://tinyurl.com/y5uaq6rs

Sorry, The Comment Form Is Temporarily Closed At This Time
You may also like

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More