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