Why does redirecting "sudo echo" stdout to a file not give root ownership?

Posted by orokusaki on Super User See other posts from Super User or by orokusaki
Published on 2012-12-06T02:24:48Z Indexed on 2012/12/06 5:09 UTC
Read the original article Hit count: 560

Filed under:
|
|

I'm pretty new to using Linux heavily, and I'm trying to learn more about file ownership and permissions. One thing that I stumbled on just now was exactly what the title says, running:

weee@my-server:~$ sudo echo "hello" > some-file.txt
weee@my-server:~$ ls -lh
total 4.0K
-rw-rw-r-- 1 weee weee 6 Dec  5 21:21 some-file.txt

The file is owned by me, whereas touch works like one would expect:

weee@my-server:~$ sudo touch other-file.txt
weee@my-server:~$ ls -lh
total 4.0K
-rw-r--r-- 1 root    root    0 Dec  5 21:22 other-file.txt

How can I force the file to be created by root? Do I simply have to create the file in my homedir, then sudo chown root... and sudo mv ... move it to /var where I need it to be? I was hoping there'd be a single step to accomplish this.

© Super User or respective owner

Related posts about linux

Related posts about sudo