Yes, we can save command output by redirecting it to a file. The standard streams for input, output, and error are as follows (also known as file descriptors):
Advertisements
- stdin (numeric value 0) – Keyboard
- stdout (numeric value 1) – Screen/display
- stderr (numeric value 2) – Screen/display

So 1 represents stdout and 2 represents stderr. Based upon the information we can:
- Redirect stdout/stderr to a file
- Redirect stdout to a stderr OR redirect stderr to a stdout
- To redirect stderr and stdout to a file
- We can redirect stderr and stdout to stdout too
- And finally you can redirect stderr and stdout to stderr
How to save terminal output to a file
By default, the command sends outputs to stdout and can be redirected to the file using the following syntax:
For example, save the date command output to a file named mydate.txt, run:
To view file contains use the cat command:
command > filename.txtFor example, save the date command output to a file named mydate.txt, run:
date > mydate.txtTo view file contains use the cat command:
cat mydate.txtFeed data to our commands (input redirection)
We can read input from a file using the following simple syntax and the file must already exist:
command < input.txt
cat < /etc/passwd
wc -l < /etc/passwdAppend output to a file
If the filename.txt/mydate.txt (file) already exists, it will get overwritten. To append output, run:
Verify it:

Please note that the file such as mydate.txt is overwritten unless the bash noclobber option is set using the set command. For example, turn off noclobber option:
Sample outputs:
command >> filename.txt
echo "------------------" >> mydate.txt
ls -l /etc/resolv.conf >> mydate.txtVerify it:
cat mydate.txt
Please note that the file such as mydate.txt is overwritten unless the bash noclobber option is set using the set command. For example, turn off noclobber option:
set -o noclobber
echo "some data" > mydata.txtSample outputs:
bash: mydata.txt: cannot overwrite existing file
We can turn on noclobber option as follows:
set +o noclobber
echo "foo bar" > mydata.txtHow to redirect stderr to a file
The syntax is as follows:
OR
Above works with bash and other modern shell. For POSIX version try:
In this example, send the find command errors to a file named err.log:
Verify it:
Sample outputs:
command &> file.txt
command &>> file.txtOR
command 2> file.txt
command 2>> file.txtAbove works with bash and other modern shell. For POSIX version try:
command >output.txt 2>&1
command >>output.txt 2>&1In this example, send the find command errors to a file named err.log:
find / -iname "*.conf" &>err.log
## OR ##
find / -iname "*.conf" 2>err.log
## POSIX version ##
find . -iname "*.conf" >err.log 2>&1Verify it:
cat err.logSample outputs:
find: ‘./snap.chromium’: Permission denied find: ‘./systemd-private-timesyncd.service-KOh0jg’: Permission denied find: ‘./snap.demo’: Permission denied find: ‘./snap.lxd’: Permission denied find: ‘./.vbox-root-ipc’: Permission denied
How to suppress error messages
Use the following syntax:
We can also redirect error messages (stderr) to standard output (stdout), run:
command 2>&-
find . -iname "*.txt" 2>&-We can also redirect error messages (stderr) to standard output (stdout), run:
command 2>&1
echo "foo" 2>&1
kill $target_pid 2>&1 > /dev/nullHow to redirect both stdout and stderr to a file
The syntax is as follows to redirect both stdout and stderr to a file:
For example:
To append text to end of file use the following syntx:
command 2>&1 | tee output.txtFor example:
find . -iname "*.txt" 2>&1 | tee cmd.log
cat cmd.logTo append text to end of file use the following syntx:
find . -iname "*.conf" 2>&1 | tee -a cmd.log
cat cmd.logHow to combine redirections
The following command example simply combines input and output redirection. The file resume.txt is checked for spelling mistakes, and the output is redirected to an error log file named err.log:
spell < resume.txt > error.logHow to redirect screen output (stdout) and errors (stderr) to /dev/null
Try the following syntax
command > /dev/null 2>&1
/path/to/script.py > /dev/null 2>&1Redirect both standard error and standard out messages to a log file
command > log.txt 2>&1
/path/to/my-appname.py > my-appname.log 2>&1Conclusion
You learned how to save terminal output to a file when using Linux or Unix-like operating system with modern shell such as Bash or KSH including POSIX syntax. See bash docs here for more info or type the following man command:
man bash
No hay comentarios:
Publicar un comentario