Cat is the most popular command used in Linux to display the file content on the console window.
Lets learn with below examples one by one
cat abc.txt will display the content from file abc.txt on the console.
cat bcd.txt cde.txt will display the content from files bcd.txt and cde.txt on the console
cat pr1.pl pr2.pl > pr3.pl will display the content form files pr1.pl and pr2.pl and also writes the concatenated content of both files in pr3.pl file
cat a.js > b.js will create a new file b.js and copies the content from a.js into b.js file. If b.js file already exists it will overwrite the content from a.js file.
cat a.pl >> b.pl will append the content of a.pl and the end of b.pl and displays
cat list | sort will sort the content from list in alphabetical order and then displays
cat foobar.js | grep foo will display only the lines containing word foo from the file foobar.js on the console
cat -n program will display the content with line numbers for both empty and non empty lines
cat -b program will display the content with line numbers for only non empty lines.
cat -ne program will display the content with line numbers as well as the end of line with a dollar symbol
cat -T prg.pl will display the TAB characters if used any in file on the console. It will display TAB by ^I.
cat << EOF
It is used to read the content from stdin until it matches with specific pattern EOF and will display on the console
tac pg.js will display the content in reverse order
Note: Files with any file extension or without extension can be given to cat command.
Lets learn with below examples one by one
cat abc.txt will display the content from file abc.txt on the console.
cat bcd.txt cde.txt will display the content from files bcd.txt and cde.txt on the console
cat pr1.pl pr2.pl > pr3.pl will display the content form files pr1.pl and pr2.pl and also writes the concatenated content of both files in pr3.pl file
cat a.js > b.js will create a new file b.js and copies the content from a.js into b.js file. If b.js file already exists it will overwrite the content from a.js file.
cat a.pl >> b.pl will append the content of a.pl and the end of b.pl and displays
cat list | sort will sort the content from list in alphabetical order and then displays
cat foobar.js | grep foo will display only the lines containing word foo from the file foobar.js on the console
cat -n program will display the content with line numbers for both empty and non empty lines
cat -b program will display the content with line numbers for only non empty lines.
cat -ne program will display the content with line numbers as well as the end of line with a dollar symbol
cat -T prg.pl will display the TAB characters if used any in file on the console. It will display TAB by ^I.
cat << EOF
It is used to read the content from stdin until it matches with specific pattern EOF and will display on the console
tac pg.js will display the content in reverse order
Note: Files with any file extension or without extension can be given to cat command.