Cannot echo "hello" > x.txt even with sudo? [duplicate]

Solution 1:

The redirection is done by the shell before sudo is even started. So either make sure the redirection happens in a shell with the right permissions

sudo bash -c 'echo "hello" > f.txt'

or use tee

echo "hello" | sudo tee f.txt  # add -a for append (>>)