03. Changing Permissions chmod

To change the mode of a file or directory, we use the chmod command.

Note that only the file's owner or the superuser is able to change the mode of a file.

There are two ways of using chmod - by octal number or symbolic representation.

Chmodding by Octal

Octal values range from 0-7, rather than 0-9 in the more familiar decimal number system.

To see how chmodding works in octal, we must convert first to binary.

The 1 represents a boolean true value, while a 0 represents false, which is then applied to our rwx pattern for permissions.

OctalBinaryFile Mode
0000---
1001--x
2010-w-
3011-wx
4100r--
5101r-x
6110rw-
7111rwx

Now you can see how an octal value can be translated to all file permissions for either owner, group or all.

To use chmod, we pass in file mode in octal values for owner, group, in order, followed by the file we'd want to modify.

$ chmod 644 helloWorld.txt

Here, we would set the file accession of the owner to 6, or rw-, and 4, or r-- for both the group and others (all).

This would give us the new file attributes -rw-r--r--.

As another example, the command below would change the helloWorld.txt accession to -r---wxr--. Can you see why?

$ chmod 434 helloWorld.txt

Most common permission modes

It'd be a hassle to memorize all octal values. Here are a list of absolute permission modes that are used most often.

OctalBinary
644rw- --r --r
600rw- --- ---
755rwx -wx -wx
700rwx --- ---
711rwx --x --x

Chmodding by Symbolic Representation

The second way to chmod is by symbolic representation. If you have trouble remembering the octal forms, this may be a better alternative for you.

We have four characters to describe the file user, group, others and all.

u
User (file owner).
g
Group.
o
Others (world).
a
All (u+g+o).
[none]
If no characters are present, all is assumed.

Authorizing or revoking permissions

We may use the plus (+) and minus (-) symbols to add or remove the permissions rwx.

To give or revoke permissions from multiple groups, simply separate the permissions with a comma (,).

Here are some common examples of chmodding by symbol. We'll start with the default 644 permissions (-rw-r--r--).

$ touch helloWorld.txt
$ ls -l helloWorld.txt
-rw-r--r-- 1 JohnDoe staff 0 Apr 1 23:20 helloWorld.txt
# Give group permission to execute $ chmod g+x helloWorld.txt $ ls -l helloWorld.txt
-rw-r-xr-- 1 JohnDoe staff 0 Apr 1 23:20 helloWorld.txt
# Revoke read permissions from others $ chmod o-r helloWorld.txt $ ls -l helloWorld.txt
-rw-r-x--- 1 JohnDoe staff 0 Apr 1 23:20 helloWorld.txt
# Give all execution permission $ chmod +x helloWorld.txt $ ls -l helloWorld.txt
-rwxr-x--x 1 JohnDoe staff 0 Apr 1 23:20 helloWorld.txt
# Revoke execution permission from all and give read permission to others $ chmod a-x,o+r helloWorld.txt $ ls -l helloWorld.txt
-rw-r--r-- 1 JohnDoe staff 0 Apr 1 23:20 helloWorld.txt

Specifying exact permissions

In addition to + and -, we can assign precise permissions with the equals symbol (=).

# All can only read
$ chmod =r helloWorld.txt
$ ls -l helloWorld.txt
-r--r--r-- 1 JohnDoe staff 0 Apr 1 23:20 helloWorld.txt

This will change our mode of helloWorld.txt to only read.

$ chmod g=rwx helloWorld.txt
$ ls -l helloWorld.txt
-rw-rwxr-- 1 JohnDoe staff 0 Apr 1 23:20 helloWorld.txt

Note that the settings for umask may alter what you'd expect for the chmod command above. Let's learn about umask in our next lesson to better understand this.

Aching back from coding all day?

Inversion Therapy Table

Aching back from coding all day? Try Back Problems

Stretch out your back and relieve your back muscles with inversion therapy. This device counteracts the forces of gravity on the body by decompressing and elongating the spine. By using this product just ten minutes a day, you can be well on your way to improved circulation and posture while relieving muscle aches, back pain and stress.

$$ Check price
119.98119.98Amazon 4.5 logo(1,700+ reviews)

More Back Problems resources

Take your Linux skills to the next level!

Command Line Kung Fu

Take your Linux skills to the next level! Try Linux & UNIX

Command Line Kung Fu is packed with dozens of tips and practical real-world examples. You won't find theoretical examples in this book. The examples demonstrate how to solve actual problems. The tactics are easy to find, too. Each chapter covers a specific topic and groups related tips and examples together.

$ Check price
14.9914.99Amazon 4.5 logo(27+ reviews)

More Linux & UNIX resources

Ad