Linux – chown Command

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.



Tutorials dojo strip

Initial Example

To change the owner of a file to a specific user, you would use:

chown username filename.txt




CHOWN Parameters

ParameterDescription
-c, --changesReport only when changes are made, providing feedback similar to verbose mode.
-f, --silent, --quietSuppress most error messages to reduce output clutter.
-v, --verboseDisplay a detailed diagnostic message for every file processed.
--dereferenceChange the ownership of the target of each symbolic link (default behavior).
-h, --no-dereferenceChange the ownership of the symbolic link itself, rather than the file it points to.
--from=CURRENT_OWNER:CURRENT_GROUPOnly change ownership if the current owner and/or group match the specified values.
--no-preserve-rootDo not treat the root directory (/) specially, which is the default behavior.
--preserve-rootPrevent any recursive operations on the root directory (/).
--reference=RFILEUse the ownership of RFILE as a reference instead of specifying new owner and group values.
-R, --recursiveApply changes to files and directories recursively.
-HTraverse directories specified as symbolic links in command-line arguments.
-LFollow and traverse every symbolic link to directories encountered during the operation.
-PDo not follow any symbolic links during the traversal (default behavior).
--helpDisplay help information and exit.
--versionShow 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




Linux Playground

Scroll to Top