What it does
chown USER FILE gives a file to another user; chown USER:GROUP FILE also changes the group; chown :GROUP FILE changes only the group. Only root may change the owner.
With -R it works on a whole tree. Numbers (UID/GID) work as well as names, which helps when the user does not exist on this system (like files from a backup or NFS).
How to write it
chown [OPTION]... [OWNER][:[GROUP]] FILE...
chown [OPTION]... --reference=RFILE FILE...Main parts
USER- Only the owner changes.
USER:GROUP- Owner and group change.
:GROUP- Only the group changes (same as chgrp).
USER:- Owner changes; group becomes the login group of USER.
Options 15
Most used
Nothing matches this filter.
Examples 11
$ run as a normal user# run as root (or with sudo)
1Give a file to a user
chown lisa report.txtOnly root can do this.
2Set owner and group
chown lisa:sales report.txtBoth in one step.
3Change only the group
chown :sales report.txtSame as
chgrp sales report.txt.4Give a web app to the web server user
chown -R apache:apache /var/www/html/app/uploadsOnly the upload folder; the code itself should stay owned by root (hardening).
5Show only what changed
chown -Rc lisa:lisa /home/lisa changed ownership of '/home/lisa/old.txt' from root:root to lisa:lisaGood after you copied files as root into a home folder.
6Change only files of an old user
chown -R --from=olduser newuser /srv/projectFiles of other owners stay as they are.
7Use numbers for files from another server
chown -R 1500:3000 /srv/restoreWorks even when the names do not exist here.
8Copy the owner of another file
chown --reference=/var/www/html/index.html /var/www/html/new.htmlSame owner and group as the reference.
9Change a symbolic link itself
chown -h lisa /home/lisa/link-to-dataWithout -h the target file changes.
10Fix files after a UID change
find / -xdev -uid 1500 -exec chown -h sam {} +All files of the old UID go to sam.
11Protect against a typo on /
chown -R --preserve-root lisa / chown: it is dangerous to operate recursively on '/'Use it in scripts with variables.
Nothing matches this filter.
Watch out
chown -R user /(for example from an empty variable$DIR/) breaks the system. Check paths in scripts.- A normal user cannot give files away.
Exit codes
0 | Done. |
1 | Error. |