cat command tutorial in linux/unix with examples and use cases

linux cat command – concatenate files and print on the standard output.

It can also display multiple files concatenated together, and using the shell, its standard output can be redirected from the screen to the file.

syntax

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
  • -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

Copy standard input to standard output.

➜  ~ cat

Add the line number of the file(including the blank line) to another file

➜  ~ cat -n examples.txt > test.txt 

Add the line number of the file (excluding the blank line) and enter it into another file.

➜  ~ cat -b examples.txt > test.txt

Append file content to another file content

➜  ~ cat examples.txt >> test.txt 
>      :overwrite write
>>      :append write

Empty file contents

In Linux, the null device is basically utilized for discarding of unwanted output streams of a process, or else as a suitable empty file for input streams. This is normally done by redirection mechanism.

And the /dev/null device file is therefore a special file that writes-off (removes) any input sent to it or its output is same as that of an empty file.

➜  ~ cat /dev/null > test.txt 
In server operation, we usually use this method to clear the log file. 
Of course.
You can also use the echo command to achieve the same purpose.

 ➜  ~ echo "" > test.txt
  OR 
 ➜  ~ echo > test.txt                

Combine multiple file contents into one file

➜  ~ cat examples.txt examples-1.txt > test.txt 

To view multiple files

➜  ~ cat examples.txt examples-1.txt 

Add a Comment

Your email address will not be published.