How to redirect the information output by the terminal into the file?

Time:2021-9-22

How to save the terminal output to a file when using bash / KSH / CSH / tcsh on Linux, MacOS, * BSD or UNIX like operating systems?

standard output
2> Error output
< standard input
How to save terminal output to a file
Use the > standard output symbol to import the output information into the time.txt file, for example:

[[email protected] ~]# date > time.txt
[[email protected] ~]# cat time.txt
Tue May 19 14:09:57 CST 2020
How to save the content output by the Linux terminal to the file? How to save the content output by the Linux terminal to the file

Redirect content input to commands
Use the < standard input symbol to enter a file into the command list, for example:

[[email protected] ~]# cat < /etc/passwd
[[email protected] ~]# wc -l < /etc/passwd
How to save the content output by the Linux terminal to the file? How to save the content output by the Linux terminal to the file

Append output to file
Using > > overwrites the file, and using > > appends the contents of the file. For example:

[[email protected] ~]# echo ‘————‘ >> time.txt
How to save the content output by the Linux terminal to the file? How to save the content output by the Linux terminal to the file

Output errors to a file
For example, output the error information to err.log and the correct result to info.txt. You can do this:

[[email protected] ~]$ find / -iname “*.conf” 2> err.log > info.txt
How to save the content output by the Linux terminal to the file? How to save the content output by the Linux terminal to the file
For example, put the error message and the correct result in the same file, which can be done as follows:

[[email protected] ~]$ find / -iname “*.conf” &> info.txt

perhaps

[[email protected] ~]$ find / -iname “*.conf” > info.txt 2>&1
How to save the content output by the Linux terminal to the file? How to save the content output by the Linux terminal to the file

summary
For more information, use man bash to see more.