The Unix and Linux cat command is the most universal and powerful tool. It can be used to display the content of a file, copy content from one file to another. The contents of multiple files, display the line number, display $ at the end of the line, etc.
Sample syntax of cat command :
# cat <file name>
like example of cat command :
# cat doc
Linux cat command use with many other options
1.cat > [fileName] To create a file.
2.cat [oldfile] > [newfile] To copy content from older to new file.
3.cat [file1 file2 and so on] > [new file name] To concatenate contents of multiple files into one.
4.cat -n/cat -b [fileName] To display line numbers.
5.cat -e [fileName] To display $ character at the end of each line.
6.cat [fileName] <<EOF Used as page end marker.
Linux cat command to create a file
The ‘cat’ command can be used to create a new file with greater than sign (>).
Sample syntax of cat command to create a new file :
# cat > <file name>
Like example of cat command :
See your terminal after run the command, we have created a new file called “test”. Now let’s see how to create it.
Append the Content of A File
Unix or Linux cat command with double greater than sign (>>) append add something in the last of a file. You can try with example below.
Sample syntax of append content in a file :
Sample syntax of append with symbol >> :
# cat <content file name> >> <append file name>
Like example of append :
# cat doc >> test
Linux cat command to concatenate files
The Unix and Linux cat command can be used to concatenate the contents of multiple files in a single new file.
Sample Syntax of the multiple apply :
# cat <file name1> <file name2> > <new file name>
Like example :
# cat test test1 > test2
Linux cat -n command to display line numbers
Linux ‘cat -n’ option displays line numbers in front of each line in a file.
Sample syntax of cat -n command :
# cat -n <file name>
Like example of cat -n :
# cat -n test
Above the picture you can see the line number in the file content. Blank number also can be show line number.
Linux cat -b file name command
The ‘cat -b’ option removes the empty lines.
Sample syntax of cat -b command :
# cat -b <file name> [ b means blank line]
Like example of cat – b command :
# cat -b test
Above the picture blank line can’t show line number.
Linux cat -e command to display ( $ )
The Linux system cat-e option displays a $ sign at the end of every line.
Sample syntax of cat -e command :
# cat -e <file name>
Like example of cat -e command :
# cat -e test
Above the file you can see, every line space show dollar
‘$’ sign.
Linux cat command as an end marker
Linux ‘cat << EOF ‘ option displays an end marker at the end of a file. It is called here directive and file content will be saved at the given end marker.
The file can be saved with the help of ‘ctrl + d ‘ keys also. It works like the end marker.
Sample syntax of cat <<EOF command :
# cat << EOF
Like example of cat <<EOF :
# cat > test2 << EOF
Linux cat filters command
When cat command is used inside pipes, it can to help filters.
Sample syntax cat command filters :
# cat | cat or tac | cat or tac |. . .
Like example of cat filters command :
# cat test.txt | tac | cat | cat | tac
This command help to you filtering system on your file line and file arguments.