Cat Command in Linux/Unix With Examples | Code Factory


Donate : Link

Medium Blog : Link

Applications : Link

cat is a standard Unix utility that reads files sequentially, writing them to standard output. The name is derived from its function to concatenate files.

cat (concatenate) command is very frequently used in Linux. It reads data from the file and gives their content as output. It helps us to create, view, concatenate files. So let us see some frequently used cat commands.

Usage: cat [OPTION]... [FILE]...
Concatenate FILE(s) to standard output.

With no FILE, or when FILE is -, read standard input.

  -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

Examples:
  cat f - g  Output f's contents, then standard input, then g's contents.
  cat        Copy standard input to standard output.

1. View single file

~/codeFactory$ cat test.txt 

emp001 aaa IT 11000-1000 2018
emp002 aba HR 10000-1500 2015
emp003 abc IT 15000-2200 2021
emp004 xyz ACC 13000-1400 2017
emp005 qwe HR 9000-500 2019

2. View multiple files

~/codeFactory$ cat test.txt test2.txt 

emp001 aaa IT 11000-1000 2018
emp002 aba HR 10000-1500 2015
emp003 abc IT 15000-2200 2021
emp004 xyz ACC 13000-1400 2017
emp005 qwe HR 9000-500 2019
Narendra 8
Amit 4
Rahul 5

3. View contents of a file preceding with line numbers.

~/codeFactory$ cat -n test2.txt 

     1  Narendra 8
     2  Amit 4
     3  Rahul 5

4. Create file

press the CTRL + D to save the file

~/codeFactory$ cat > test4

Hi CodeFactory

5. Copy the contents of one file to another file

~/codeFactory$ cat test.txt > test3.txt 

~/codeFactory$ cat test3.txt 

emp001 aaa IT 11000-1000 2018
emp002 aba HR 10000-1500 2015
emp003 abc IT 15000-2200 2021
emp004 xyz ACC 13000-1400 2017
emp005 qwe HR 9000-500 2019

6. Cat command can suppress repeated empty lines in output

~/codeFactory$ cat test3.txt 
1
2

3


4




5

~/codeFactory$ cat -s test3.txt 
1
2

3

4

5

7. Cat command can append the contents of one file to the end of another file

~/codeFactory$ cat test3.txt 
Hi CodeFactory...


~/codeFactory$ cat test.txt >> test3.txt 

~/codeFactory$ cat test3.txt 
Hi CodeFactory...

emp001 aaa IT 11000-1000 2018
emp002 aba HR 10000-1500 2015
emp003 abc IT 15000-2200 2021
emp004 xyz ACC 13000-1400 2017
emp005 qwe HR 9000-500 2019

8. Cat command can display content in reverse order using tac command

~/codeFactory$ cat test2.txt 
Narendra 8
Amit 4
Rahul 5

~/codeFactory$ tac test2.txt 
Rahul 5
Amit 4
Narendra 8

9. Cat command to merge the contents of multiple files

~/codeFactory$ cat test.txt test2.txt > test3.txt 

~/codeFactory$ cat test3.txt 
emp001 aaa IT 11000-1000 2018
emp002 aba HR 10000-1500 2015
emp003 abc IT 15000-2200 2021
emp004 xyz ACC 13000-1400 2017
emp005 qwe HR 9000-500 2019
Narendra 8
Amit 4
Rahul 5

10. Cat command to display the content of all text files in the folder

~/codeFactory$ cat *.txt
emp001 aaa IT 11000-1000 2018
emp002 aba HR 10000-1500 2015
emp003 abc IT 15000-2200 2021
emp004 xyz ACC 13000-1400 2017
emp005 qwe HR 9000-500 2019
Narendra 8
Amit 4
Rahul 5

11. Cat command to write in an already existing file

~/codeFactory$ cat test2.txt 
Narendra 8
Amit 4
Rahul 5

~/codeFactory$ cat >> test2.txt 
Manmohan 2

~/codeFactory$ cat test2.txt 
Narendra 8
Amit 4
Rahul 5
Manmohan 2

Leave a comment