How to save terminal output to a file under Linux/Unix

ADVERTISEMENTS



stdin (numeric value 0) – Keyboard stdout (numeric value 1) – Screen/display stderr (numeric value 2) – Screen/display

How do I save the terminal output to a file when using BASH/KSH/CSH/TCSH under Linux, macOS, *BSD or Unix-like operating systems?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):

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

So 1 represents stdout and 2 represents stderr. Based upon the information we can:

By default, the command sends outputs to stdout and can be redirected to the file using the following syntax:

command > filename.txt

For example, save the date command output to a file named mydate.txt, run:

date > mydate.txt

To view file contains use the cat command:

cat mydate.txt

Feed 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/passwd

Append output to a file

If the filename.txt/mydate.txt (file) already exists, it will get overwritten. To append output, run:

command >> filename.txt

echo "------------------" >> mydate.txt

ls -l /etc/resolv.conf >> mydate.txt

Verify it:

cat mydate.txt



set -o noclobber

echo "some data" > mydata.txt

bash: mydata.txt: cannot overwrite existing file

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:

We can turn on noclobber option as follows:

set +o noclobber

echo "foo bar" > mydata.txt

How to redirect stderr to a file

The syntax is as follows:

command &> file.txt

command &>> file.txt

OR

command 2> file.txt

command 2>> file.txt

Above works with bash and other modern shell. For POSIX version try:

command >output.txt 2>&1

command >>output.txt 2>&1

In 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>&1

Verify it:

cat err.log

Sample 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:

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/null

How to redirect both stdout and stderr to a file

The syntax is as follows to redirect both stdout and stderr to a file:

command 2>&1 | tee output.txt

For example:

find . -iname "*.txt" 2>&1 | tee cmd.log

cat cmd.log

To append text to end of file use the following syntx:

find . -iname "*.conf" 2>&1 | tee -a cmd.log

cat cmd.log

How 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.log

Try the following syntax

command > /dev/null 2>&1

/path/to/script.py > /dev/null 2>&1

Redirect 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>&1

Conclusion

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