Permission control on who can access files, search directories and run scripts.

Permissions govern who can read, write, create or execute files and directories.

Use ls -l to check the permissions of the files and directories of the working directory.'

A file's permissions might look like this:

- rwx rw- r--

The first character identifies the file type. - is a file, d a directory.
The following nice characters identify the read, write and execute permissions for each user group.

chmod Syntax

chmod needs three arguments to set permissions, who, what and which.

Who are the permissions set for

u User The owner of the file.
g Group Members of the group the file belongs to.
o Others Everyone else who isn't governed by u and g permissions.
a All All of the above.

If none of these are used, chmod behaves as if a had been used.

What changes are intended

Removes a permission.
+ Grants a new permission in addition to existing ones.
= Sets a permission and removes others.

Which permissions is being set

r Read The file can be opened, and its content viewed.
w Write The file can be edited, modified, and deleted.
x Execute If the file is a script or a program, it can be executed.

Examples

chmod +x filename

Grants everyone execution permission for filename

chmod o-r filename

Removes the owner's permission to read filename

Numerical Shorthands

Each permission combination is represented by the binary equivalent of a number between 0 and 7.

# Binary Permission
0 000 No Permission
1 001 Execute permission
2 010 Write permission
3 011 Write & Execute permissions
4 100 Read permission
5 101 Read & Execute permissions
6 110 Read & Write permissions
7 111 Read, Write & Execute permissions

When the numerical shorthand, permissions can't be added but are set for each group in form of a three digit number.

Examples

chmod 664 filename

Grants the user and group members permission to read & write, but only read to everyone else.

chmod 777 filename

Grants everyone permission to read, write and execute filename

find -type d | while read directory; do chmod 755 "$directory"; done

Recursively gives directories read & execute privileges.

find -type f | while read file; do chmod 644 "$file"; done

Recursively gives files read privileges.

incoming(3) | trabant | pass | bash