Linux cat Command
The cat (full English: concatenate) command is used to concatenate files and print them to the standard output device. Its main purpose is to view and concatenate files.
Permissions
All users
Syntax
cat [选项] [文件]
Parameter description:
-n: Display line numbers, adding a line number before each line of output.-b: Display line numbers, but number only non-blank lines.-s: Squeeze consecutive blank lines into a single blank line.-E: Display at the end of each line$the symbol.-T: Display Tab characters as^I。-v: Display some non-printing characters.
Usage instructions:
Display file content:
cat filenameIt will output the content of the specified file to the terminal.Concatenate files:
cat file1 file2 > combined_fileYou can concatenate the contents of file1 and file2 and output the result to combined_file.Create a file: you can use
catcommand to create a file, for examplecat > filename, then you can enter text and pressCtrl+Dto save and exit.Display file in terminal: you can use
catwith a pipe (|) in combination to display the output of other commands, for examplels -l | catwillls -l's output throughcatprinted to the terminal.
Examples
View file content:Displays the content of the file filename.
cat filename
Create a file:Redirect standard input to the file filename, overwriting the file's content.
cat > filename
Append content to a file:Append standard input to the end of the file filename.
cat >> filename
Concatenate files:Merge the contents of file1 and file2 into file3.
cat file1 file2 > file3
Display the contents of multiple files:Display the contents of file1 and file2 at the same time.
cat file1 file2
Using pipes:Use the output of the cat command as input to another command.
cat filename | command
View the last few lines of a file:Displays the last 10 lines of the file filename.
cat filename | tail -n 10
Use the -n option to display line numbers:Displays the content of the file filename and adds a line number before each line.
cat -n filename
Use the -b option to display line numbers only for non-blank lines:
cat -b filename
Use the -s option to merge blank lines:Displays the content of the file filename and collapses consecutive blank lines.
cat -s filename
Use the -t option to display tab characters:Displays the content of the file filename, using ^I to represent tab characters.
cat -t filename
Use the -e option to display end-of-line characters:Displays the content of the file filename, using $ to represent the end of a line.
cat -e filename
Add line numbers to the contents of textfile1 and output to textfile2:
cat -n textfile1 > textfile2
Add line numbers to the contents of textfile1 and textfile2 (excluding blank lines) and then 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 a floppy disk image, insert the floppy 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:
- 1. OUTFILE refers to the output image file name.
- 2. IMG_FILE refers to the image file.
- 3. When writing back from the image file to the device, the device capacity must be equivalent.
- 4. Usually used to create boot disks.
Linux Command Reference