How to use the cat command in Linux


The cat command (short for “concatenate”) lists the contents of files to the terminal window. This is faster than opening the file in an editor, and there’s no chance you can accidentally alter the file.

Display Contents of File

passwd file
cat /etc/passwd
View Contents of Multiple Files in terminal
cat test test1
cat --help

Display Contents of File. Awaits input from user, type desired text and press CTRL+D (hold down Ctrl Key and type ‘d‘) to exit. The text will be written in test2 file.

test file
cat >test
cat test
ssss ssss

With files longer than the number of lines in your terminal window, the text will whip past too fast for you to read. You can pipe the output from cat through less to make the process more manageable. Type q to quit from less.

cat .bashrc | less
# User specific aliases and functions
alias rm='rm -i'
Display Line Numbers in File
cat -n .bashrc
Display Tab separated Lines in File
cat -T .bashrc
Sorting Contents
cat test test1 test2 | sort > test4

The syntax for the cat command is as follows:

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

-A, --show-all
equivalent to -vET
-b, --number-nonblank
number nonempty output lines, overrides -n
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
equivalent to -vT
-T, --show-tabs
display TAB characters as ^I
-v, --show-nonprinting
use ^ and M- notation, except for LFD and TAB
display this help and exit
output version information and exit

Related Tags

#Linux# #cat#