Permission denied when trying to append a file to a root owned file with sudo [closed]
This is the shell command that results in "Permission denied" when I'm trying to append the data in a file to another file with sudo:
sudo cat add_file >> /etc/file
The file at /etc/file
is owned by root
(i.e. me) and its permissions are rw-r--r--
. Should I become root
for a moment to make it work or is there a workaround for sudo
?
Run bash
as sudo
:
$ sudo bash -c "cat add_file >> /etc/file"
$ whoami;sudo bash -c "whoami";whoami
iiSeymour
root
iiSeymour
Try doing this instead :
sudo tee -a /etc/file < add_file
It's lighter than running bash or sh -c command
A funny possibility is to use ed
(the standard editor):
sudo ed -s /etc/file <<< $'r add_file\nwq'
I know I won't get upvoted for this wonderful answer, but I wanted to include it here anyway (because it's funny). Done!