In this post I'm going to show you my favorite way of logging the output of a command to a log file.
# Types of Output
There are two types of output that are important for us,
stdout (1) and
stdout to a File
So the simplest way to redirect all output of a command to a given log file is to use the
command > all.log
stderr to a File
To redirect only the errors of a command to a given file you can use
command 2> error.log
stderr to separate Files
You can combine both syntaxes to separate the two and write into different log files.
command > stdout.log 2> stderr.log
stderr to the same File
Of course you can also redirect the outputs into the same file.
command > all.log 2>&1
2>&1 rewrites the output of
stderr (2) to
# Redirect to a
If you basically want to ignore some or all output of a command, you can redirect it to a
null device instead of a file.
command > /dev/null command 2> /dev/null command > /dev/null 2>&1
tee to write to File and to Screen
The downside of the methods shown is that they do no longer write the given output to the screen. But there is
tee for the rescue! This command writes to a given file and still writes to stdout 🤩
command |& tee all.log
|& is shorthand for
And that's it! 😎