The chown command in Unix-like operating systems is used to change the owner and group of files and directories. This cheat sheet covers essential chown commands for managing ownership and group assignments.

Introduction:

The chown command is a fundamental tool for changing the owner and group of files and directories on Unix-based systems. It allows users to transfer ownership and assign group membership.

Basic Commands:

CommandDescription
chown [owner]:[group] [file/directory]Change both owner and group.
chown :[group] [file/directory]Change only the group.
chown [owner] [file/directory]Change only the owner.

Recursive Changes:

CommandDescription
chown -R [owner]:[group] [directory]Recursively change owner and group for a directory and its contents.
chown -R [owner] [directory]Recursively change only the owner for a directory and its contents.
chown -R :[group] [directory]Recursively change only the group for a directory and its contents.

Numerical User and Group IDs:

CommandDescription
chown [owner_id].[group_id] [file/directory]Change owner and group using numerical IDs.
chown [owner_id] [file/directory]Change only the owner using a numerical ID.
chown .[group_id] [file/directory]Change only the group using a numerical ID.

Examples:

CommandDescription
chown user:group file.txtChange owner to user and group to group for file.txt.
chown -R root:admin /etc/configRecursively change owner to root and group to admin for /etc/config and its contents.
chown 1000:1000 /home/user/documentsChange owner and group to user with UID/GID 1000 for /home/user/documents.

Miscellaneous:

CommandDescription
chown --reference=[reference_file] [file/directory]Copy ownership from a reference file.
chown --from=[old_owner]:[old_group] [file/directory]Change ownership based on existing owner/group.
chown --versionDisplay the chown version information.
chown --helpDisplay help and usage information for chown.

Conclusion

The chown command is essential for managing file and directory ownership on Unix-like systems. This cheat sheet provides essential commands for changing owners and groups, performing recursive changes, using numerical IDs, and more. Whether you are a regular user or a system administrator, understanding these chown commands will help you manage file ownership efficiently.