Chmod Command - Change File Permissions | Online Free DevTools by Hexmos

Learn how to use the chmod command to change file permissions. Understand user, group, and others permissions with practical examples and octal notation.

Chmod Command

The chmod command is a fundamental utility in Unix-like operating systems used to change the access permissions of files and directories. Understanding how to manage these permissions is crucial for system security and proper file management. This guide explains the various ways to use chmod, including symbolic and octal notations.

Understanding File Permissions

File permissions are divided into three categories: the owner of the file (u), the group the file belongs to (g), and others (o). For each category, there are three types of permissions: read (r), write (w), and execute (x).

Using Symbolic Notation

Symbolic notation allows you to add, remove, or set permissions using letters. The syntax is generally chmod [who][operator][permissions] [file].

  • Who: u (user), g (group), o (others), a (all - ugo)
  • Operator: + (add), - (remove), = (set exactly)
  • Permissions: r (read), w (write), x (execute)

Examples of Symbolic Notation:

# Give the [u]ser who owns a file the right to e[x]ecute it.
chmod u+x PATH

# Give the [u]ser rights to [r]ead and [w]rite to a file/directory.
chmod u+rw PATH

# Remove e[x]ecutable rights from the [g]roup.
chmod g-x PATH

# Give [a]ll users rights to [r]ead and e[x]ecute.
chmod a+rx PATH

# Give [o]thers (not in the file owner's group) the same rights as the [g]roup.
chmod o=g PATH

# Remove all rights from [o]thers.
chmod o= PATH

# Change permissions recursively, allowing [g]roup and [o]thers to [w]rite.
chmod -R g+w,o+w PATH

# Add the execute permission bit to directories only. This works because the
# 'X' is uppercase, meaning only directories will be executable. However, if
# an existing file is executable, this bit will not be removed.
chmod a+X PATH

Using Octal (Numeric) Notation

Octal notation represents permissions using numbers. Each permission type (r, w, x) is assigned a value: r=4, w=2, x=1. These values are summed for each category (user, group, others).

  • 7 (4+2+1) = read, write, execute
  • 6 (4+2) = read, write
  • 5 (4+1) = read, execute
  • 4 (4) = read
  • 0 = no permissions

The format is chmod [octal_permissions] [file], where the three digits represent user, group, and others, respectively.

Example of Octal Notation:

# Set access rights using numeric (octal) form (user: rwx, group: r-x, others: ---).
chmod 750 PATH

Converting Between Notations

You can use external tools or online converters to switch between symbolic and octal representations of permissions.

# Convert string representation of the access right into numeric form and back.
curl cheat.sh/chmod/750
curl cheat.sh/chmod/rwxr-x---

Further Resources

For more in-depth information on file permissions and the chmod command, consult the following resources: