chown_and_chmod_examples
This is an old revision of the document!
Table of Contents
CHOWN and CHMOD Examples
Change the owner of a file
# ls -lart tmpfile [ls is used to diplay the file/directory properties] -rw-r--r-- 1 himanshu family 0 2012-05-22 20:03 tmpfile # chown root tmpfile # ls -l tmpfile -rw-r--r-- 1 root family 0 2012-05-22 20:03 tmpfile
Change the group of a file
Through the chown command, the group (that a file belongs to) can also be changed. # ls -l tmpfile -rw-r--r-- 1 himanshu family 0 2012-05-22 20:03 tmpfile # chown :friends tmpfile # ls -l tmpfile -rw-r--r-- 1 himanshu friends 0 2012-05-22 20:03 tmpfile
Change both owner and the group
# ls -l tmpfile -rw-r--r-- 1 root family 0 2012-05-22 20:03 tmpfile # chown himanshu:friends tmpfile # ls -l tmpfile -rw-r--r-- 1 himanshu friends 0 2012-05-22 20:03 tmpfile
Change owner only if a file is owned by a particular user
Using chown “–from” flag, you can change the owner of a file, only if that file is already owned by a particular owner.
# ls -l tmpfile -rw-r--r-- 1 root friends 0 2012-05-22 20:03 tmpfile # chown --from=guest himanshu tmpfile # ls -l tmpfile -rw-r--r-- 1 root friends 0 2012-05-22 20:03 tmpfile # chown --from=root himanshu tmpfile # ls -l tmpfile -rw-r--r-- 1 himanshu friends 0 2012-05-22 20:03 tmpfile
- In the example above, we verified that the original owner/group of the file ‘tmpfile’ was root/friends.
- Next we used the ‘–from’ flag to change the owner to ‘himanshu’ but only if the existing owner is ‘guest’.
- Now, as the existing owner was not ‘guest’. So, the command failed to change the owner of the file.
- Next we tried to change the owner if the existing owner is ‘root’ (which was true) and this time command was successful and the owner was changed to ‘himanshu’.
chown_and_chmod_examples.1470816394.txt.gz · Last modified: 2023/03/09 22:35 (external edit)