Javatpoint Logo
Javatpoint Logo

Cat Command in Linux/Unix with Examples

The 'cat' command is the most universal and powerful tool. It is considered to be one of the most frequently used commands. It can be used to display the content of a file, copy content from one file to another, concatenate the contents of multiple files, display the line number, display $ at the end of the line, etc.

Cat can be described as a standard Unix utility that sequentially reads files, writing them on the standard output. The name is derived from the function to catenate files. It has been shipped to several operating systems. This command is used to concatenate files which include multiple files into one file.

Usage of the Cat Command

The Single Unix Specification describes the cat operation to read files in the order provided in its arguments, specifying their contents in the same order as the standard output. The specification mandates the one option flag support, i.e., u (unbuffered output), meaning that all bytes are specified after it has been read. By default, a few operating systems do it and avoid the flag, such as the ones with the GNU Core Utilities.

If any input filename is described as one hyphen, the cat command reads through standard input in the order at that point. The cat command only reads from standard input if no file is mentioned.

Use Cases of the Cat Command

The cat command can be used for piping a file to any program that expects binary data or plain text on the input stream. The cat command doesn't damage non-text bytes when outputting and concatenating. As such, the two primary use cases of this command are certain format-compatible binary file types and text files.
Text concatenation is restricted to text files with the same legacy encoding, like ASCII. The cat command doesn't offer a way for concatenating Unicode text files that contains the files or a Byte Order Mark with distinct text encodings from each other.

The resulting merged file may not be authorized for several structured binary data sets. For instance, if a file contains a unique footer or header, the result will duplicate these. Although, for a few multimedia digital container formats, the final file is authorized, and so the cat command offers an effective appending file means. Video streams can be an important file example that the cat command can merge without problem, e.g., the DV (Digital Video) and MPEG program stream (MPEG-2 and MPEG-1) formats, which are simple packet streams fundamentally.

Options in the cat Command

  • --show-all, -A: It is the same as -vET.
  • --number-nonblank, -b: It shows the total non-empty output lines. Also, it overrides -n.
  • -e: It is the same as -vE.
  • --show-ends, -E: It shows the $ symbol at the completion of all lines.
  • --number, -n: It gives the total of every output line.
  • --squeeze-blank, -s: It suppresses redundant empty output lines.
  • -t: It is the same as -vT.
  • --show-tabs, -T: It shows TAB characters as ^|.
  • -u: ignored.
  • --show-nonprinting, -v: It uses M- and ^ notation, except TAB and LFD.
  • --version: It displays the information of the output version and exit.
  • --help: It shows the help menu and exit.

Linux cat command: to display file content

The 'cat' command can be used to display the content of a file.

Syntax:

Example:


Linux cat Display

In the above snapshot, file 'jtp.txt' is displayed with the help of command "cat jtp.txt".

Note: To display the content of multiple files at once, type file names in one single line like "cat file1 file2 file3... fileN.

Linux cat command usage

OptionFunction
cat > [fileName]To create a file.
cat [oldfile] > [newfile]To copy content from older to new file.
cat [file1 file2 and so on] > [new file name]To concatenate contents of multiple files into one.
cat -n/cat -b [fileName]To display line numbers.
cat -e [fileName]To display $ character at the end of each line.
cat [fileName] <<EOFUsed as page end marker.

Linux cat command (to create a file)

The 'cat' command can be used to create a new file with greater than sign (>).

Syntax:

Example:


Linux cat Create1

In the above snapshot, we have created a new file called "javatpoint". Now let's see how to create it.

Type the command "cat >javatpoint" and press 'enter'. You will be directed to the next line.

Press 'enter' after every line and you will be directed to the next line. To save your file, go to the next line, press 'ctrl+d' and your file will be saved.

To Append the Content of A File

The 'cat' command with double greater than sign (>>) append (add something in the last of a file) something in your already existing file.

Syntax:

Example:


Linux cat Creat2

Look at the above snapshot, a new line at the end is added in the file 'javatpoint'. After passing "cat >> javatpoint" command, type the lines as much as you want to add. To save the file press 'ctrl + d'.

Linux cat command (to copy file)

The 'cat' command can be used to copy the content of a file into another file.

Syntax:

Example:


Linux cat Copy

In the above snapshot, we have copied the content of file 'combo' into the file 'combo2' with the command "cat combo > combo2".

Linux cat command (to concatenate files)

The 'cat' command can be used to concatenate the contents of multiple files in a single new file.

Syntax:

Example:


Linux cat concatenate1

Look at the above snapshot, we have combined three files "file1, file2, and file3" into a single file "combo" with the command "cat file1 file2 file3 >combo".

Notice the content of three separate files and then the content of a new concatenated file that is "combo".

To Insert A New Line

A new line will be inserted while concatenating multiple files by using a hyphen (-).

syntax:

Example:


Linux cat concatenate2

In the above snapshot, we have inserted a new line at the beginning while concatenating file1, file2 and file3 with the command "cat - file1 file2 file3 >combo".

Note: Line will be inserted at the beginning of the file only.

Linux cat -n command (to display line numbers)

The 'cat -n' option displays line numbers in front of each line in a file.

Syntax:

Example:


Linux cat Display Line Number1

Look at the above snapshot; the file 'jtp.txt' has a line number in front of every line by passing the command "cat -n jtp.txt".

cat -b (file name)

The 'cat -b' option removes the empty lines.

Syntax:

Example:


Linux cat Display Line Number2

In the previous snapshot, after line 19, line number 20 has also been marked but it is an empty line.

In the above snapshot, line 20 is removed with the help of command "cat -b jtp.txt".

Linux cat -e command (to display $)

The 'cat-e' option displays a '$' sign at the end of every line.

Syntax:

Example:


Linux cat e

Look at the above snapshot; some lines include spaces also. A user won't be able to recognize whitespace at the end of each line. The "cat -e program" command will put the $ sign at the end of every line including spaces.

Linux cat command (as an end marker)

The 'cat << EOF ' option displays an end marker at the end of a file. It is called here directive and file content will be saved at the given end marker.

The file can be saved with the help of 'ctrl + d ' keys also. It works like the end marker.

Note: Any word other than 'EOF' can be used for the end marker.

Syntax:

Example:


Linux cat end marker

In the above snapshot, we have created 'exm.txt' file with 'EOF' as the end marker by passing the command "cat > exm.txt << EOF".

Linux Cat Filters

When cat command is used inside pipes, it does nothing except moving stdin to stout.

Syntax:

Example:


Linux Cat Filter

Look at the above snapshot, the output of one 'cat' or 'tac' command is passing onto another as input.

Show All File Contents

The basic usage of the cat command is showing the file contents. Simply, we need to type "cat" followed by the filename to show the file contents using the terminal window:

We can apply the wildcard character along with the cat command to show every file in the current directory:

To show only the contents of a text file inside a directory, we can type the below command:

Show Multiple Files

Also, we can combine and show multiple file contents together inside the terminal window with the help of the cat command. We can use the below syntax to simultaneously show multiple files:

Copy the Result of a File to Another

The cat command can also be used to copy the result of a file to another. First, it creates it if not found. It overwrites the desired file otherwise. We can use the below syntax to copy the result of a file to another:

Append the Result of One File to Another

Rather than overwriting the result of a desired file in the old example, we can also use the cat command for appending the result:

If the destination file doesn't exist, it creates it. It appends the result otherwise.

Sort the Result

Also, we can merge the sort using the cat command for alphabetically sorting the result:


Next TopicLinux tac





Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA