Skip to content

Instantly share code, notes, and snippets.

@brpaz
Created October 31, 2014 19:52
Show Gist options
  • Save brpaz/4ecc920f8e7db7c815f9 to your computer and use it in GitHub Desktop.
Save brpaz/4ecc920f8e7db7c815f9 to your computer and use it in GitHub Desktop.
redirect output with sudo #sudo #tee #linux #stdout

Run a shell with sudo and give the command to it by using the -c option:

sudo sh -c 'ls -hal /root/ > /root/test.out'

Use sudo tee (if you have to escape a lot when using the -c option):

sudo ls -hal /root/ | sudo tee /root/test.out > /dev/null

The redirect to /dev/null is needed to stop tee from outputting to the screen. To append instead of overwriting the output file, use tee -a or tee --append.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment