Get the App
SLTechnology News&Howtos  ›  Servers  › 

How to record terminal output to text file in Centos

Shulou Source: shulou.com Published: 2022-06-01 14:32:31 09月27日 Update

This article introduces the relevant knowledge of "how to record terminal output to text files in Centos". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

First, save the result of the command run to a file: use > to turn the output.

Example:

$ls > ls.txt # or ls-- > ls.txt # saves the running result of the ls command to the file ls.txt

Note: > directs the output to the specified file. If the file already exists, it will be rewritten, and the original contents of the file will not be retained.

The output is attached to the back of the file, and the original contents of the file will be retained.

Second, record the information into a file while outputting it: tee command

Explain what tee does:

"read from standard input and write to standard output and files", which reads from standard input and writes it to standard output and files

Parameter:-a,-- append, "append to the given FILEs, do not overwrite", append to the given file instead of overwriting it

Example:

$ls | tee ls.txt # will display the execution result of the ls command on the terminal and output the execution result to the ls.txt file

$ls | tee-a ls.txt # retains the original contents of the ls.txt file and adds the execution result of the ls command to the end of the ls.txt file.

Third, the output of multiple commands needs to be recorded: script command

The command script is very powerful and can record all the output of the terminal to the corresponding file.

Example:

1. $script

Script. Started, file is typescript

2. $ls

…… Content omission

3. $exit

Exit

Script. Done, file is typescript

4. $cat typescript # will display the green part on it again:

Description:

1. We didn't specify a file name when we started script, which is automatically recorded in a file called typescript under the current directory. You can also specify a file name with the-a parameter

Example:

$script. -a the output of the example.txt # terminal is recorded in the example.txt file

2. When you exit script, use exit. In fact, script starts a shell.

This is the end of the content of "how to record terminal output to a text file in Centos". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Tags: File output content command terminal result example text information parameter file name more standard knowledge run practical powerful learned and then facts Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Huawei OPPO Reno vpn Shulou Tech Info Shulou Technology