10/31/2014 - 7:52 PM

redirect output with sudo #sudo #tee #linux #stdout

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.