# Cheatsheet & Examples: chmod

chmod is a command-line utility used to change the access permissions of file system objects such as files and directories. It allows you to control who can read, write, and execute those objects.

## Changing Permissions Using Octal Notation

Example Usage:
`chmod 755 myfile.txt`

What it does:
Sets the permissions of `myfile.txt`. The owner gets read, write, and execute permissions (7), the group gets read and execute permissions (5), and others get read and execute permissions (5).

Command-line Arguments Explained:

- `755`: The octal representation of the desired permissions. The first digit (7) represents the owner's permissions, the second digit (5) represents the group's permissions, and the third digit (5) represents the others' permissions.  Each digit is calculated as the sum of its permission bits:
    - 4 (read)
    - 2 (write)
    - 1 (execute)
    - 7: (4+2+1) Read, write, and execute
    - 5: (4+0+1) Read and execute
- `myfile.txt`: The name of the file whose permissions are to be changed.

## Changing Permissions Using Symbolic Notation

Example Usage:
`chmod u+rwx,g+rx,o+rx myfile.txt`

What it does:
Grants the owner read, write, and execute permissions, the group read and execute permissions, and others read and execute permissions to `myfile.txt`.

Command-line Arguments Explained:

- `u`: Represents the user/owner.
- `g`: Represents the group.
- `o`: Represents others (all users not in the owner or group).
- `+`: Adds the specified permissions.
- `-`: Removes the specified permissions.
- `=`: Sets the specified permissions, removing all others.
- `r`: Read permission.
- `w`: Write permission.
- `x`: Execute permission.
- `rwx`: Combines read, write, and execute permissions.
- `myfile.txt`: The name of the file whose permissions are to be changed.

## Removing Permissions

Example Usage:
`chmod u-w myfile.txt`

What it does:
Removes write permission from the owner of `myfile.txt`.

Command-line Arguments Explained:

- `u`: Represents the user/owner.
- `-`: Removes the specified permissions.
- `w`: Write permission.
- `myfile.txt`: The name of the file whose permissions are to be changed.

## Granting Execute Permission to a Directory

Example Usage:
`chmod +x mydirectory`

What it does:
Grants execute permission to all users on the directory `mydirectory`.  This is necessary to allow users to `cd` into the directory and access its contents.

Command-line Arguments Explained:

- `+`: Adds the specified permissions.
- `x`: Execute permission.
- `mydirectory`: The name of the directory whose permissions are to be changed.

## Setting Permissions Recursively

Example Usage:
`chmod -R 755 mydirectory`

What it does:
Applies the permissions `755` to `mydirectory` and all files and subdirectories within it, recursively.

Command-line Arguments Explained:

- `-R`: (Recursive) Applies the changes to the directory and all of its contents.
- `755`: The octal representation of the desired permissions.
- `mydirectory`: The name of the directory whose permissions are to be changed.

## Changing the Owner of a File

Example Usage:
`chown user:group myfile.txt`

What it does:
Changes the owner and group ownership of `myfile.txt`.  Note: This uses the `chown` command, which is often used in conjunction with `chmod`.

Command-line Arguments Explained:

- `user`: The username of the new owner.
- `:group`: The name of the group to which the file will belong.
- `myfile.txt`: The name of the file.

## Preserving the Existing Permissions and Adding

Example Usage:
`chmod +a "group:mygroup allow read,write,execute" myfile.txt`

What it does:
Adds Access Control List (ACL) permissions. Note: requires `acl` support on the filesystem.

Command-line Arguments Explained:

- `+a`: Adds a new Access Control List (ACL) entry.
- `"group:mygroup allow read,write,execute"`: Specifies that the group `mygroup` has read, write, and execute permissions.
- `myfile.txt`: The name of the file whose permissions are to be changed.
