Linux cat Command
The cat (short for concatenate) command is used to concatenate files and print them to standard output; its main purpose is to view and concatenate files.
Permissions
All users
Syntax format
cat [选项] [文件]
Parameter Description:
-n: Display line numbers; a line number is added before each output line.-b: Display line numbers, but only number non-empty lines.-s: Compress consecutive empty lines so that only one is shown.-E: Display at the end of each line$symbol.-T: Display the Tab character as^I.-v: Display some non-printing characters.
Usage notes:
-
Display file contents:
cat filenameoutputs the contents of the specified file to the terminal. -
Concatenate files:
cat file1 file2 > combined_filecan concatenate the contents of file1 and file2 and output the result to combined_file. -
Create a file: you can use
catto create a file, for examplecat > filename, then you can type text and pressCtrl+Dto save and exit. -
Display a file in the terminal: you can combine
catwith a pipe (|) to display the output of other commands, for examplels -l | catwill passls -l's output throughcatand print it to the terminal.
Example
** View file contents:** Display the contents of the file filename.
cat filename
** Create a file:** Redirect standard input to the file filename, overwriting the file's contents.
cat > filename
** Append to a file:** Append standard input to the end of the file filename.
cat >> filename
** Concatenate files:** Combine the contents of file1 and file2 into file3.
cat file1 file2 > file3
** Display multiple files:** Display the contents of file1 and file2 at the same time.
cat file1 file2
** Use a pipe:** Use the output of the cat command as input to another command.
cat filename | command
** View the last lines of a file:** Display the last 10 lines of the file filename.
cat filename | tail -n 10
** Use the -n option to display line numbers:** Display the contents of the file filename and add a line number before each line.
cat -n filename
Use the -b option to display line numbers only for non-empty lines:
cat -b filename
** Use the -s option to squeeze empty lines:** Display the contents of the file filename and merge consecutive empty lines.
cat -s filename
** Use the -t option to display tabs:** Display the contents of the file filename and represent tabs with ^I.
cat -t filename
** Use the -e option to display line endings:** Display the contents of the file filename and represent the end of a line with $.
cat -e filename
Add line numbers to the contents of textfile1 and write them into textfile2:
cat -n textfile1 > textfile2
Add line numbers to the contents of textfile1 and textfile2 (excluding blank lines) and append the result to textfile3:
cat -b textfile1 textfile2 >> textfile3
Clear the contents of /etc/test.txt:
cat /dev/null > /etc/test.txt
cat can also be used to create image files. For example, to create an image of a floppy disk, insert the disk and type:
cat /dev/fd0 > OUTFILE
Conversely, if you want to write an image file to a floppy disk, type:
cat IMG_FILE > /dev/fd0
Note:
-
- OUTFILE refers to the name of the output image file.
-
- IMG_FILE refers to the image file.
-
- When writing an image file back to a device, the device's capacity must be adequate.
-
- It is usually used to create a boot disk.
Linux Command Reference