Write Errors To File

Standard errors are by default printed on the screen. However, it is often useful to write errors to a file for logging purposes. We can do that using 2>.

Create Code That Will Produce Error

ls /a/
ls: cannot access '/a/': No such file or directory

Create File To Store Error

touch errors.log

Write Error To File

In this line we run the command that produces the error and then add 2> errors.log to tell the system to write any errors to the file errors.log instead of printing them on the screen.

ls /a/ 2> errors.log

View Contents Of File To Check That The Error Was Written

cat errors.log
ls: cannot access '/a/': No such file or directory