Signup/Sign In

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:

Options Descriptions
-A, --show-all equivalent to -vET
-b, --number-nonblank number non-empty 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
-v, --show-nonprinting use ^ 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>

cat command to display line numbers

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

cat command printing non-printable characters

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>

cat command remove repeated blank or empty lines

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.



About the author:
Pradeep has expertise in Linux, Go, Nginx, Apache, CyberSecurity, AppSec and various other technical areas. He has contributed to numerous publications and websites, providing his readers with insightful and informative content.