Detailed usage of cat command in linux system

In Linux, the cat command is a very commonly used command. Its function is to output the contents of the file to the screen, or to combine multiple files into one file. Here are some common uses of the cat command:

 

​1. Display file content

Use the cat command to print out the contents of the file, such as:

cat filename

This will print all the lines of the file to the terminal.

2. Merge multiple files

You can also combine multiple files into one file using the cat command, such as:

cat file1 file2 > file3

This will merge the contents of file1 and file2 into a new file file3.

3. Use pipes to output content

In the Linux environment, the output of one command can be used as the input of another command using a pipeline, such as:

cat filename | grep keyword

This will output the line containing keyword in filename.

4. Compress the file

The cat command can also be used to compress files. Redirecting the cat output into a tar command can compress the file into a tar file like:

cat file1 file2 | tar -czf archive.tar.gz -

This will pack file1 and file2 into one tar file, which is then compressed using gzip.

5. Redirect output

The output of the cat command can be redirected to a file using redirection symbols, such as:

 cat filename > newfile

This will output the contents of filename into newfile, or create a new file if newfile does not exist.

The above is the common usage of cat command.

 

Guess you like

Origin blog.csdn.net/tiansyun/article/details/130037643