The chown
(short for “change owner”) command is used to change the ownership of files and directories. This command allows you to set the user and group ownership, which is crucial for managing permissions and access control in a multi-user environment.
Initial Example
To change the owner of a file to a specific user, you would use:
chown username filename.txt
CHOWN Parameters
Parameter | Description |
---|---|
-c, --changes | Report only when changes are made, providing feedback similar to verbose mode. |
-f, --silent, --quiet | Suppress most error messages to reduce output clutter. |
-v, --verbose | Display a detailed diagnostic message for every file processed. |
--dereference | Change the ownership of the target of each symbolic link (default behavior). |
-h, --no-dereference | Change the ownership of the symbolic link itself, rather than the file it points to. |
--from=CURRENT_OWNER:CURRENT_GROUP | Only change ownership if the current owner and/or group match the specified values. |
--no-preserve-root | Do not treat the root directory (/ ) specially, which is the default behavior. |
--preserve-root | Prevent any recursive operations on the root directory (/ ). |
--reference=RFILE | Use the ownership of RFILE as a reference instead of specifying new owner and group values. |
-R, --recursive | Apply changes to files and directories recursively. |
-H | Traverse directories specified as symbolic links in command-line arguments. |
-L | Follow and traverse every symbolic link to directories encountered during the operation. |
-P | Do not follow any symbolic links during the traversal (default behavior). |
--help | Display help information and exit. |
--version | Show version information and exit. |
Examples
1. Changing the Owner of a File
To change the owner of a file to a specific user:
chown john file.txt
2. Changing the Owner and Group of a File
To change both the owner and group of a file:
chown john:staff file.txt
3. Changing the Owner of a Directory and Its Contents
To change the owner of a directory and all its contents, use the -R
(recursive) option:
chown -R john /path/to/directory
4. Using the --reference
Option
To set the owner and group of a file based on another file:
chown --reference=reference_file target_file
5. Changing the Group Only
To change the group ownership of a file without changing the owner:
chown :staff file.txt