Flash Sale:75% Off Hosting + Free DomainEnds in13h47m14sView Plans
Hostvento logoHostvento

Linux cat Command Usage with Examples

Use cat to show, create, join and number the lines of text files.

How-To Guides2 min read

What is cat

cat (short for concatenate) can:

  • Display a text file on the screen.
  • Create a new text file.
  • Read a text file.
  • Add to a file.
  • Join several files.

Syntax

cat [options] filename

Examples

Show a file

cat filename

Create a file

cat > file1.txt

Type your text, then press Ctrl+D to save:

This is my new file in Linux.
The cat command is very useful.
Thanks

Read the file

cat file1.txt

Join files

If you have two files:

cat sample1.txt
This is my first sample text file

cat sample2.txt
This is my second sample text file

Join them into a new file:

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

The output shows the content of both files.

Show all .txt files

cat *.txt

Number the lines

cat -n file1.txt

Output:

1 This is my new file in Linux.
2 The cat command is very useful.
3 Thanks

Use -b to number only the lines that are not empty.

Copy a file into another

cat file2.txt > file1.txt
Warning: > overwrites the target file.

Append to a file

cat file2.txt >> file1.txt

>> adds to the end without deleting anything.

Show line ends and tabs

cat -A file1.txt

$ marks the end of each line.

Squeeze blank lines

cat -s file1.txt

Use cat with a pipe

cat access.log | grep 404

(grep 404 access.log does the same without cat.)

Common options

OptionMeaning
-nNumber all lines
-bNumber non-empty lines
-sSqueeze repeated empty lines
-EShow $ at the end of each line
-AShow all special characters

Quick recap

  • cat file shows a file, cat a b > c joins files.
  • > overwrites, >> appends.