Use
The cat command in Linux is used to concatenate files and print their content on the screen. The most common use is to view the contents of any file.
Syntax
cat [OPTION]… [FILE]…
Example
COnsider a file name sep.txt having some content in it. Then we can use the cat command to view its content as shown below.
To concatenate, specify the file names by separating them with space. Suppose you want to view the contents of two files sep.txt and mon.txt then use the cat command as
The cat command concatenates the contents of two files and prints it on the screen. Further, you can redirect the concatenation of multiple files into a new file. This help you to club content of multiple files into one file by using the cat comamnd.
$cat sep.txt mon.txt > target.txt
Different ways of using cat command in Linux
cat command can also be used to redirect content into a file.
$cat > file1
The above command allows you to type the content on the screen. To stop typing press Ctril-D. The typed entire will get saved into the file named file1. Remember, it will overwrite any existing content within the file.
Another method of using cat command is to append content to a file.
$cat >> file1
The above command allows you to type the content on the screen. To stop typing press Ctril-D. All the content that you type will get appended into the file named file1. This does not overwrite the existing content.
Options used with cat command in Linux
1. Show line numbers
-n: option displays line numbers along with each line.
2. Show $ at the end of each line
-E: option displays the $ sign at the end of each output line.
3. Squeeze multiple blank/empty lines
-s: At times a file might contain consecutive blank lines. -s option clubs these multiple blank lines into one.
Practice question on using cat command in Linux
Q1. Club the content of all the .txt files into a file named ALL.txt.
Q2. Add the content of File1 at the end of the file File2 using the cat command.