chmod command

Please give and explain 10 examples of the chmod command.

The chmod command in Unix and Unix-like operating systems is used to change the permissions of files and directories. It allows you to specify who can read, write, and execute a file or directory. The chmod command uses a numeric or symbolic representation to set permissions. Here are 10 examples of how to use the chmod command, along with explanations:

  1. Numeric Representation - Absolute Mode:

    chmod 755 file.txt

    Explanation: This command sets the permissions for file.txt to rwxr-xr-x, which means the owner can read, write, and execute, while others can only read and execute.

  2. Numeric Representation - Octal Mode:

    chmod o+w file.txt

    Explanation: This adds write permission for others (world) to file.txt.

  3. Symbolic Representation - Add and Remove Permissions:

    chmod u+x,g=rw,o-r file.txt

    Explanation: This command adds execute permission for the owner, sets read and write permissions for the group, and removes read permission for others on file.txt.

  4. Symbolic Representation - Recursive Change:

    chmod -R u=rwX,g=rX,o=rX directory/

    Explanation: This recursively sets read and write permissions for the owner and read permissions for the group and others on all files and directories within directory/. The capital 'X' allows execute permission only for directories and files that already have execute permission.

  5. Symbolic Representation - Absolute Mode with Sticky Bit:

    chmod +t directory/

    Explanation: This sets the sticky bit on the directory, which means only the owner can delete their own files within the directory.

  6. Numeric Representation - Set UID and Set GID:

    chmod u+s,g+s file

    Explanation: This sets the set-user-ID (SUID) and set-group-ID (SGID) bits on file, allowing it to run with the permissions of the owner or group, respectively, when executed.

  7. Symbolic Representation - All Permissions for Owner:

    chmod u=rwx file

    Explanation: This command gives the owner full read, write, and execute permissions on file.

  8. Numeric Representation - Remove All Group and Other Permissions:

    chmod go= file

    Explanation: This removes all permissions for the group and others on file.

  9. Symbolic Representation - Inheritance from Parent Directory:

    chmod g+s directory/

    Explanation: This sets the SGID bit on directory, causing new files and subdirectories created within it to inherit the group ownership of the parent directory.

  10. Symbolic Representation - Advanced Usage with Multiple Permissions:

    chmod u=rwx,go=rx file

    Explanation: This command explicitly sets read, write, and execute permissions for the owner and read and execute permissions for the group and others, effectively removing write permissions for the group and others.

Remember that the exact behavior of the chmod command may vary slightly between different Unix-like systems, so it's a good practice to refer to the system's documentation or man pages for more details on specific options and behaviors.

You should also read: