cat command is used to view the content of a file without opening it in any text editor. Another major use of the cat command is to concatenate multiple files into a single file.
parameter
parameter | meaning |
---|---|
-n |
Show line numbers (empty lines are also numbered) |
-s |
Merge multiple empty lines into one line |
-b |
Show line numbers (empty lines are not numbered) |
-e |
Display $ at the end of each line |
--help |
show help |
--version |
show version information |
grammar
cat <option> <filename1 filename2 filename3>
example
View Files
cat 123.txt
View all files in the current folder
cat *
View all files of the specified format in the current folder
cat *.txt
Display multiple files at once
cat t1.txt t2.txt
Copy output from one file to another
If the target file does not exist, it will first create it, otherwise append the contents to the target file.
cat t1.txt > t2.txt
Append the output of one file to another
If the target file does not exist, it will first create it, otherwise it will overwrite the target file.
cat t1.txt >> t2.txt
Append the sequential output of multiple files to another file
cat t1.txt t2.txt >> t3.txt
content filtering
#在error.log文件中不包含org.apache.catalina.authenticator的行
cat -n error.log |grep -v 'org.apache.catalina.authenticator'
#在error.log文件中包含org.apache.catalina.authenticator的行,并显示后面的5行信息
cat -n error.log |grep 'org.apache.catalina.authenticator' -A 5
#在error.log文件中包含org.apache.catalina.authenticator的行,并显示前面的5行信息
cat -n error.log |grep 'org.apache.catalina.authenticator' -B 5
#在error.log文件中包含org.apache.catalina.authenticator的行,并显示前、后的5行信息
cat -n error.log |grep 'org.apache.catalina.authenticator' -C 5