Menu

Advanced cat Command Usage in Linux

How to print or concatenate files in linux with cat command? (Advanced)

cat is one basic and most used command by Linux users. It simply prints the contents of a file or STDIN as output on the terminal. But that is not the full power of cat command. There are many advanced uses of cat which are rarely known.

> Check your top used commands using this command: history | awk '{CMD[$2]++;count++;}END { for (a in CMD)print CMD[a] " " CMD[a]/count*100 "% " a;}' | grep -v "./" | column -c3 -s " " -t | sort -nr | nl | head -n10

Based syntax of cat command:

cat [OPTION]... [FILE]...

Description of options available in cat command:

OptionsDescriptions
-A, --show-allequivalent to -vET
-b, --number-nonblanknumber non-empty output lines, overrides -n
-eequivalent to -vE
-E, --show-endsdisplay $ at end of each line
-n, --numbernumber all output lines
-s, --squeeze-blanksuppress repeated empty output lines
-tequivalent to -vT
-T, --show-tabsdisplay TAB characters as ^I
-v, --show-nonprintinguse ^ and M- notation, except for LFD and TAB

Show line number with cat command in Linux

To display line numbers in front of each line with **cat** command we use the **-n** (--number) option.

Syntax: cat -n <fileName>

To display line numbers in front of each non-empty line with cat command we use the -b (--number-nonblank) option.

Syntax: cat -b <filename> or cat -n -b <filename>

Show all characters with cat command in Linux

To display non-printable characters with cat command we have the following options: -A, -v, -E, -T.

Non-printable characters are parts of a character set that do not represent a written symbol or part of the text within a file or code. These characters tells a program like cat about how a document is supposed to look.

1. Show end character at the end of each line with cat command in Linux

To display To display non-printable characters with catcommand we have the following options:-A, -v, -E, -T`.

Non-printable characters are parts of a character set that do not represent a written symbol or part of the text within a file or code. These characters tells a program like cat about how a document is supposed to look.

1. Show end character at the end of each line with cat command in Linux

To display after the last character of each line with cat command we use the -e (--show-ends) option.

Syntax: cat -E <filename>

2. Show TAB character with cat command in Linux

To display TAB characters as ^I with cat command use the -T (--show-tabs) option.

Syntax: cat -T <filename>

3. Show non-printing characters with cat command in Linux

To display Non-printable characters which use ^ and M- notation, except for LFD and TAB (e.g. color deciding characters) use -v (--show-nonprinting) option.

Syntax: cat -v <filename>

> Tip: -A is equivalent to -vET and -t is equivalent to -vT and -e is equivalent to -vE.

Remove repeated empty lines with cat command in Linux

To suppress annoying repeated empty lines with cat command with -s (--squeeze-blank)option.

Syntax: cat -s <filename>

Conclusion

In this tutorial, you have learned some advanced usage of cat command. To show line numbers, non-printable characters and squeeze unwanted empty lines.