howtouselinux

10 Examples to use Linux Cat Command

Table of Contents

Cat command is used to concatenate files and to view the contents of files on the standard output in Linux. The following 10 examples can help us understand how to use Linux cat command.

  • Display a File Using cat
  • Cat Command with More & Less Options
  • Show Line Numbers using Cat
  • Display non-printing characters using Cat
  • Reducing Blank Lines with Cat
  • Copy the contents of one file to another
  • Appending the contents of one file to another
  • View the contents of many files with Cat
  • Redirecting the output of multiple files into a single file
  • Sorting Contents of Multiple Files in a Single File

Linux Cat command Options

  • -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

Display a File Using Linux cat command

The cat command is one of the most frequently used commands in Linux. It allows you to view the contents of a file. The syntax for using the cat command is:

cat filename

where filename is the name of the file you want to view.

To see an example of how the cat command works, let’s create a file called myfile.txt and add some text to it:

echo “This is my file” > myfile.txt

Now, if we use the cat command to view the contents of myfile.txt, we should see the following output:

This is my file

As you can see, the cat command simply displays the contents of the file.

Show Line Numbers using Linux Cat command

If there are lines with no characters at all they won’t be numbered. For all the non-empty lines in a file use the following command:

cat -b filename

To show numbers for all the lines regardless as to whether they are blank, type the following command:

cat -n filename

Display non-printing characters using Linux Cat command

cat -e filename

Reducing Blank Lines with Linux Cat command

When we show the contents of a file using the cat command we probably don’t want to see when there are loads of consecutive blank lines. Use the -s switch to condense all blank lines into a single blank line:

cat -s filename

Copy the contents of one file to another with Linux cat command

cat filename > filename-new

Appending the contents of one file to another with Linux cat command

cat filename >> filename-new

View the contents of many files with Linux Cat command

If you want to view the contents of multiple files, you can specify them all on the command line:

cat file1.txt file2.txt file3.txt

Combine multiple files into a single file with Linux cat command

The cat command can also be used to concatenate (combine) files. For example, if we have two files, file1.txt and file2.txt, and we want to create a new file that contains the contents of both files, we can use the cat command like this:

cat file1.txt file2.txt > newfile.txt

This will create a new file called newfile.txt that contains the contents of both file1.txt and file2.txt.

Sorting Contents of Multiple Files in a Single File with Linux cat command

cat filename1 filename2 filename3|sort > filename-new

Linux Cat Command with More & Less Options

cat filename|more

cat filename|less

Related: 10 Examples to use Linux Cat Command

David Cao
David Cao

Hey there! I am David, a Cloud & DevOps Enthusiast and 18 years of experience as a Linux engineer. I work with AWS, Git & GitHub, Linux, Python, Ansible, and Bash. I am a technical blogger and a Software Engineer, enjoy sharing my learning and contributing to open-source.