Exploring Cat Command in Linux

Updated: 4 days ago

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



Cat 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 cat

The cat command displays a file to the screen.

cat filename

Cat Command with More & Less Options

cat filename|more
cat filename|less

Show Line Numbers using Cat

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 Cat

cat -e filename

Reducing Blank Lines with Cat

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

cat filename > filename-new

Appending the contents of one file to another

cat filename >> filename-new

View the contents of many files with Cat

cat filename1 filename2 filename3

Redirecting the output of multiple files into a single file

cat filename1 filename2 filename3 > filename-new

Sorting Contents of Multiple Files in a Single File

cat filename1 filename2 filename3|sort > filename-new


97 views
udemylinux.png

Join our newsletter! Get a free cloud server!  Never miss a post!

Thanks for submitting!