The chmod command in Unix-like operating systems is used to change the permissions of a file or directory. This cheat sheet covers essential chmod commands for managing file and directory permissions.

Introduction:

chmod is a command-line utility that allows users to change the permissions of files and directories on Unix-based systems. It uses a symbolic or octal notation to specify permission settings.

Basic Commands:

CommandDescription
chmod [permissions] [file/directory]Change permissions using symbolic notation.
chmod [octal_permissions] [file/directory]Change permissions using octal notation.
chmod +[permission] [file/directory]Add a specific permission.
chmod -[permission] [file/directory]Remove a specific permission.

Symbolic Notation:

SymbolPermission
uUser/Owner
gGroup
oOthers
aAll (u+g+o)
rRead
wWrite
xExecute

Examples – Symbolic Notation:

CommandDescription
chmod u+r file.txtAdd read permission for the owner.
chmod go-w directory/Remove write permission for group and others.
chmod a+x script.shAdd execute permission for all users.

Octal Notation:

OctalPermission
4Read
2Write
1Execute
0No Permission

Examples – Octal Notation:

CommandDescription
chmod 644 file.txtGive read and write permission to the owner and read-only permission to others.
chmod 755 script.shGive read, write, and execute permission to the owner, and read/execute permission to group and others.
chmod 600 sensitive_file.txtRestrict permissions to read and write only for the owner.

Recursive Changes:

CommandDescription
chmod -R [permissions] [directory]Recursively change permissions for a directory and its contents.

Special Permissions:

CommandDescription
chmod +s executableSet the setuid/setgid bit.
chmod +t directorySet the sticky bit.

Miscellaneous:

CommandDescription
chmod --reference=[reference_file] [file/directory]Copy permissions from a reference file.
chmod --versionDisplay the chmod version information.
chmod --helpDisplay help and usage information for chmod.

Conclusion

The chmod command is a powerful tool for managing file and directory permissions on Unix-based systems. This cheat sheet provides essential commands using both symbolic and octal notation, covering basic permissions, recursive changes, special permissions, and more. Whether you are a regular user or a system administrator, understanding these chmod commands will help you control access to files and directories effectively.