The chown (change owner) command is used to change the ownership of files and directories in a Linux filesystem.
-
Syntax:
chown [options] [owner][:group] file...
-
Examples:
chown alice file.txt
- Changes the owner of
file.txttoalice.
chown alice:developers project/
- Changes the owner of the
project/directory toaliceand the group todevelopers.
chown :developers file.txt
- Changes the group ownership of
file.txttodevelopers.
sudo chown -R bob /var/www/
- Recursively changes the ownership of all files and directories in
/var/www/tobob.
- Changes the owner of
-
-Ror--recursive:- Recursively changes ownership for all files and directories within the specified directory.
chown -R alice:developers /home/alice
- Applies ownership changes to all files and subdirectories in
/home/alice.
-
--reference=[rfile]:- Sets ownership of a file or directory to match the ownership of another file.
chown --reference=reference.txt file.txt
- Sets the ownership of
file.txtto match that ofreference.txt.
-
-hor--no-dereference:- Changes the ownership of symbolic links themselves, rather than the files they point to.
chown -h alice symlink
- Changes the owner of the
symlinksymbolic link toalice.
-
-vor--verbose:- Verbosely displays the ownership changes being made.
chown -v alice file.txt
- Outputs the ownership change for
file.txttoalice.
-
--help:- Displays help information about the
chowncommand.
chown --help
- Shows usage information and options.
- Displays help information about the
-
Changing Owner and Group Together:
- Specify both the owner and group separated by a colon.
chown bob:staff file.txt
- Changes the owner to
boband the group tostaff.
-
Skipping Group Change:
- Use
chown owner: fileto change only the owner without affecting the group.
chown alice: file.txt
- Changes the owner to
aliceand leaves the group unchanged.
- Use
-
Using Wildcards:
- Apply ownership changes to multiple files using wildcards.
chown bob *.txt- Changes the owner of all
.txtfiles in the current directory tobob.
-
Recursive Ownership Changes:
- Be cautious with the
-Roption, especially when running asroot, as it can modify ownership of many files and directories.
- Be cautious with the
The chown command is a powerful tool for managing file and directory ownership in Linux. It allows you to change the owner, group, or both, of files and directories. Understanding how to use chown effectively is crucial for maintaining proper access control and security on your system.