Quantcast
Channel: User Kamil Maciorowski - Super User
Viewing all articles
Browse latest Browse all 837

Answer by Kamil Maciorowski for How to append text to the end of a file as root user?

$
0
0

In sudo echo …>> … The redirection (>>) is set up by the shell before sudo is started. If echo was about to open the file by itself, it could; but it is about to start with stdout inherited from sudo, which in turn starts with stdout set up by the invoking shell. The error you got means the shell was denied access to the file.

Use sudo tee. The point is tee can open files by itself:

echo "something" | sudo tee -a /output/file > /dev/null

This way tee will append (-a) the text to the /output/file with proper permissions.

tee is designed to pass its input and duplicate it (in general: multiply). In this case one copy goes to the file and the other travels along the pipe. Since we only need the first one, we redirect the second copy to /dev/null so it doesn't appear in the console. Everything that goes to /dev/null vanishes. To open /dev/null we don't need to be root, so the fact that our unprivileged shell will open this file doesn't bother us at all.


Viewing all articles
Browse latest Browse all 837

Trending Articles