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-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
--help
display this help and exit
--version
output version information and exit

Related Tags

#Linux# #cat#