These commands are useful across all Linux distributions and can even be used on Macs and BSD. The only thing you have to think about is which command is best to get the job done.
Before you go any further, there are a few things to set up to make testing easier. As your root user, create a couple of folders in your regular user’s home directory. Change their permissions to make them unreadable.
sudo mkdir Fail1
sudo mkdir Fail2
sudo chmod 000 Fail1
sudo chmod 000 Fail2
You’ll be using the du command to test, and it will raise errors when it encounters those files.
Redirecting Output without Errors
Those will still appear in the terminal.The > operator redirects output to a file and overwrites the contents of that file.
du -h ./* > folder-size.txt
In contrast, the >> operator works the same way but appends the output to the end of the file.
du -h ./* >> folder-size.txt
Saving Only Errors
Using the 2> operator sends the errors from a command to a log file, overwriting it.
The regular output will still display in the terminal.
du -h ./* 2> errors.log
To achieve the same results without overwriting, use 2>>.
du -h ./* 2>> errors.log
Save Everything, Show Nothing
The next set of operators has you covered.
They redirect all output to files without showing anything in the terminal.
The &> operator redirects output and errors to a file and overwrites it.
du -h ./* &> disk_use.log
Like the others, if you don’t want to overwrite, there’s an accompanying operator for that.
du -h ./* &>> disk_use.log
Show Everything, Save Output
The resulting file will also only contain output and not errors.
du -h ./* | tee output.txt
By default, tee overwrites. To preserve the existing file, add the -a flag.
du -h ./* | tee -a output.txt
Show and Save It All
Finally, you can redirect with the |& operator to the tee command to display everything and log it too.
du -h ./* |& tee output.txt
To avoid overwriting, add the -a flag to tee.
du -h ./* |& tee -a output.txt
Source from: https://www.maketecheasier.com/save-output-of-command-to-file-linux/