
Fernando F. answered 06/26/19
I can teach you Unix, Database, Perl, Computers
In your example, what is happening is that the redirection portion of the command is not being executed with sudo permissions, therefore you don't have access to write to /root. Your command is the equivalent of "(sudo ls -hal /root/) > /root/test.out" (pseudocode). Since you want the 'ls' and the redirection to execute under the sudo permission you need to pass the entire command to sudo. You can execute 'sudo sh -c "ls -hal /root/ > /root/test.out" ' to write the 'ls' output to the file /root/test.out. Here you are passing the entire command to sudo so that it all executes under sudo privileges.