File Permissions Linux

Files and directories have permissions that determine who can read, write, or execute them. These permissions are split into three categories:

  1. User (u) - The owner of the file.

  2. Group (g) - Users who are members of the file's group.

  3. Others (o) - All other users.

Each category can have three types of permissions:

  • Read (r) - Allows the file to be read.

  • Write (w) - Allows the file to be modified.

  • Execute (x) - Allows the file to be executed.

Using chmod

To use chmod, you need to specify the permissions you want to set, and the file or directory to which you want to apply them. There are two main ways to specify permissions: symbolic and numeric.

Symbolic Mode

Symbolic mode uses letters to represent permissions and categories. The format is [who][operator][permission], where:

  • who: u (user), g (group), o (others), a (all)

  • operator: + (add), - (remove), = (set exactly)

  • permission: r (read), w (write), x (execute)

Examples:

  • chmod u+r file.txt - Adds read permission for the owner.

  • chmod g-w file.txt - Removes write permission for the group.

  • chmod a+x file.txt - Adds execute permission for everyone.

Numeric Mode

Numeric mode uses a three-digit octal number to represent permissions. Each digit is a sum of permission values:

  • Read = 4

  • Write = 2

  • Execute = 1

Examples:

  • chmod 755 file.txt - Sets permissions to rwxr-xr-x (read, write, execute for owner; read, execute for group and others).

  • chmod 644 file.txt - Sets permissions to rw-r--r-- (read, write for owner; read-only for group and others).

Here's a quick reference table:

Octal ValuePermissionsDescription
7rwxRead, write, execute
6rw-Read, write
5r-xRead, execute
4r--Read only
3-wxWrite, execute
2-w-Write only
1--xExecute only
0---No permissions