how to use cat command featured image

How to use cat Command?

Concatenate is the name of the command used in Linux to show the contents of a file or files in the terminal. The use of cat command can also merge files together and make new ones.

Basis Syntax:

cat [filename]

Filename can be a name of the file or the full path to a file in a different location. The content of the file is shown on the terminal.

use of cat command
Note: Simple example “cat sample.txt” shows the content inside the file “sample.txt”.

Common Options used with cat command

  • -n: All output lines are numbered with the -n option and the numbers are assigned to each new line.
use of cat command with -n flag
Details: As shown in the example “cat -n sample.txt” shows the content of the file “sample.txt” with a number for each line.
  • -s: Repeated empty output lines are suppressed using this option.
use of cat command with -s flag
Note: As seen in the example “file.txt” has multiple blank lines. With normal “cat file.txt” command the content is shown as it is. But using “cat -s file.txt” removes the multiple empty lines.
  • -E: The “$” sign, which serves as a line break indicator, is displayed at the end of each line.
use of cat command with -E flag
The only function of “-E” option in “cat -E file.txt” is to display “$” at the end of each new line. As seen in the example empty line are also counted as new line.
  • -b: Only non-blank output lines will be numbered using the -b option.
use of cat command with -b flag
Note: Using “cat -b file.txt” will display number for each new line but skip the empty lines.
  • -T: Tab characters are displayed as “^I” when the -T option is used.
cat command with -T flag
Note: The command “cat -T newfile.txt” will display the content of the file and also replace all the “tabs” to “^I”.

Uses of cat command

  • to quickly inspect the contents of a file on the terminal.
  • to display a “$” sign at the end of each line, indicating the end of the line use “-E “option.
  • The -n option is very helpful when you need to number all output lines.
  • to number non-blank output lines, the -b option is helpful.
  • to display tab characters as “^I,” the -T option is helpful.

It’s worth pointing out that the cat command can create new files by rerouting the program’s output to a new file and also concatenate files by giving multiple file names separated by spaces.

concatenate the output of both files.
“cat file1.txt file2.txt” will display the contents of both the files.
overwriting a file content
In this example “cat file2.txt >> file.txt” will append the content of “file2.txt” to “file1.txt”. Whereas “cat file2.txt > file.txt” will overwrite the content of “file1.txt” with the content of “file2.txt”.

If you want to know about all the common and important command for the Linux terminal then have a look at the article linked below.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top
Scroll to Top