shell stderr redirect

Ubuntu - Command Line Tools

# redirect standard output to file 'output.txt' (overwrite existing output.txt file)

ls > output.txt

# append standard output to existing 'output.txt' (create if not exist)

ls >> output.txt

# both standard error and standard output, redirect to file

 2>&1   redirects stderr to stdout

ls badfile > stdout_and_stderr.txt 2>&1

cat stdout_and_stderr.txt

   ls: cannot access badfile: No such file or directory

# pipe: past both standard error and output to a pipe

ls badfile 2>&1 | grep 'cannot access'

   ls: cannot access badfile: No such file or directory