Cheatsheet & Examples: chown
chown changes the ownership of files and directories. This involves modifying the user and/or group associated with a file, determining who has access and permissions.
Changing File Ownership to a Specific User
Example Usage:
chown user file.txt
What it does:
Changes the owner of file.txt to the specified user.
Command-line Arguments Explained:
- user: The username of the new owner. This can be the username itself (e.g.,
john) or the user ID (UID). - file.txt: The name of the file whose owner you want to change.
Changing File Ownership to a Specific Group
Example Usage:
chown :group file.txt
What it does:
Changes the group ownership of file.txt to the specified group.
Command-line Arguments Explained:
- :group: The name of the group that will become the new owner of the file. Note the colon (:) before the group name, which signifies that only the group is being specified. This can be the group name (e.g.,
developers) or the group ID (GID). - file.txt: The name of the file whose group owner you want to change.
Changing Both User and Group Ownership
Example Usage:
chown user:group file.txt
What it does:
Changes both the user and group ownership of file.txt.
Command-line Arguments Explained:
- user: The username or user ID of the new owner.
- :group: The groupname or group ID of the new group owner.
- file.txt: The name of the file whose owner and group you want to change.
Recursively Changing Ownership of a Directory and Its Contents
Example Usage:
chown -R user:group directory/
What it does: Recursively changes the user and group ownership of the directory and all of its contents (files and subdirectories).
Command-line Arguments Explained:
- -R: Recursive option. Applies the changes to the directory and all of its subdirectories and files.
- user: The username or user ID of the new owner.
- :group: The groupname or group ID of the new group owner.
- directory/: The name of the directory whose ownership you want to change. The trailing slash is good practice, but not strictly required.
Preserving Ownership of Symbolic Links
Example Usage:
chown -h user:group symbolic_link
What it does: Changes the ownership of the symbolic link itself, without affecting the ownership of the file or directory the symbolic link points to.
Command-line Arguments Explained:
- -h: --no-dereference - operates on the symbolic link itself instead of the file or directory it points to.
- user: The username or user ID of the new owner of the symbolic link.
- :group: The groupname or group ID of the new group owner of the symbolic link.
- symbolic_link: The name of the symbolic link.
Using chown with the --from option
Example Usage:
chown --from=olduser:oldgroup user:group file.txt
What it does:
Changes the user and group ownership only if the current user and group match olduser:oldgroup.
Command-line Arguments Explained:
- --from=olduser:oldgroup: This option specifies the existing user and group ownership that must be present before the change is made.
- user: The new username or user ID.
- :group: The new groupname or group ID.
- file.txt: The name of the file.

