How to use the cat command in Linux
Created
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
root:x:0:0:root:/root:/bin/bash bin:x:1:1:bin:/bin:/sbin/nologin
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.
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:
Usage:
cat [OPTION]... [FILE]...
OPTIONS:
-A, --show-allequivalent to -vET-b, --number-nonblanknumber nonempty 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-u(ignored)-v, --show-nonprintinguse ^ and M- notation, except for LFD and TAB--helpdisplay this help and exit--versionoutput version information and exit