Why does redirecting "sudo echo" stdout to a file not give root ownership?
- by orokusaki
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.