Cannot echo "hello" > x.txt even with sudo?
131,573
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 (>>)
Related videos on Youtube
Author by
Patryk
Software Engineer C++/Go/shell/python coder Linux enthusiast Github profiles: https://github.com/pmalek https://github.com/pmalekn
Updated on September 18, 2022Comments
-
Patryk over 1 year
Possible Duplicate:
sudo & redirect outputI'm trying to create a file in /var/www, but even with
sudo
this fails:user@debVirtual:/var/www$ sudo echo "hello" > f.txt -bash: f.txt: Permission denied
When I use
sudo nano
, I can save something to this file.Why can't I use
sudo echo
?-
Admin almost 4 years
-
-
tatsu about 5 yearswhat if you already used quote and double quote?
-
Chris B almost 5 yearsThanks. Top one didn't work for me on Debian but 2nd option worked perfectly.
-
Manachi about 3 yearsKind of lengthy + ugly really isn't it.
-
WesternGun almost 3 yearsIf you need multiple lines as stdin input, use
cat <<EOF | sudo tee -a /your/file
, and let your input's last line beEOF
.