Back to Blog

Changing Ownership and Permissions | chown, chgrp & chmod | Mac/Linux Terminal

Sandy LaneSandy Lane

Video: Changing Ownership and Permissions | chown, chgrp & chmod | Mac/Linux Terminal by Taught by Celeste AI - AI Coding Coach

Watch full page →

Changing Ownership and Permissions | chown, chgrp & chmod | Mac/Linux Terminal

Managing file ownership and permissions is crucial for system security and collaboration on Mac and Linux systems. This guide covers how to change file ownership with chown, modify group ownership using chgrp, and adjust permissions precisely with chmod using symbolic notation.

Code

# Change the owner of a file to user 'daryl'
chown daryl file.txt

# Change both owner to 'daryl' and group to 'staff'
chown daryl:staff file.txt

# Change group ownership only (equivalent to chgrp)
chown :staff file.txt

# Recursively change ownership of a directory and all contents
chown -R daryl:staff /path/to/directory

# Change group ownership of a file
chgrp staff file.txt

# Recursively change group ownership of a directory
chgrp -R staff /path/to/directory

# Add execute permission for the owner
chmod u+x file.txt

# Remove write permission from the group
chmod g-w file.txt

# Set others' permissions to read only
chmod o=r file.txt

# Combine multiple symbolic changes: add execute for owner, remove write for group, set read for others
chmod u+x,g-w,o=r file.txt

Key Points

  • chown changes file owner and optionally group; requires root or sudo for others’ files.
  • chgrp changes group ownership and is shorthand for chown :groupname.
  • Use the -R flag with chown or chgrp to apply changes recursively to directories.
  • chmod symbolic notation uses u, g, o, and a for user categories, with +, -, and = to add, remove, or set permissions.
  • Verify ownership and permissions changes with ls -l to ensure correct application.