Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Linux cat Command

Linux cat Command

Linux Command Reference Linux Command Reference

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:

Usage notes:

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:

Linux Command Reference Linux Command Reference