How to change file and folder permissions with chmod – part 1

Permissions can be set in the octal mode (with digits), or with letters.

Watch Free Movies

In this article, we will change the file and folder access rights in the human readable representation with chmod.

In Linux and Unix systems, the file and folder permission permissions are:

  • read permissions: r
  • write permissions: w
  • execution permissions : x

Structured in u g o format:

  • u = user
  • g = group
  • o = others

The user is the owner of the file or directory, the group is the group that owns the file or directory and the users that do not belong to the owner group are the others.

The generical tool for displaying the permissions is ls -l . But I prefer to use stat.

$ ls -l /etc/motd
lrwxrwxrwx 1 root root 13 2012-05-21 22:38 /etc/motd -> /var/run/motd
$ stat -c "%A %n" /etc/motd
lrwxrwxrwx /etc/motd

Sample: lrwxrwxrwx /etc/motd

The red characters represent the permissions for the user, the green characters represent the access rights for the group and the blue ones for the others.

I have this file with no access rights:

$ ls -l
---------- 1 razvan razvan 0 2012-06-29 23:12 testfile

The chmod command is used for changing the file and folder access rights. The plus + sign adds permissions and the minus – sign removes permissions:

Add exec permission for the user:
$ chmod u+x testfile
Remove exec permission for the user:
$ chmod u-x testfile

Add read and write permissions for the group:
$ chmod g+rw testfile
Remove the read and write permissions for the group:
$ chmod g-rw testfile

Add read write exec permissions for the others:
$ chmod o+rwx testfile
Remove the read write exec permissions for the others:
$ chmod o-rwx testfile

Add read write for the user, exec write for the group:
$ chmod u+rw,g+wx testfile
Remove the recently set permissions:
$ chmod u-rw,g-wx testfile

Add read write for the user and group and x for the others:
$ chmod u+rw,g+rx,o+x testfile
Remove the recently added permissions:
$ chmod u-rw,g-rx,o-x testfile

The a argument is used to set permissions to everybody (all).
Add exec and write for everybody:
$ chmod a+wx testfile
Remove the recently added permissions:
$ chmod a-wx testfile

Set read and write for the user, read and exec for the group and write for the others:
$ chmod u=rw,g=rx,o=w testfile
Remove all the permissions:
$ chmod u=,g=,r= testfile

These commands can be combined one with another, to serve your needs.

Next, I will show you how to set the permissions recursively.
To set the permissions for a directory and for all the files contained by it, use the -R option:
$ chmod -R u=rwx,g=rw,o=x testdir

This also works for removing permissions:
$ chmod -R u-x,g-x,o-rwx

Set exec permissions only for the directories.
This command will add execution writes for the user on directories in your current folder, and will not change the file permissions:
$ chmod u+X *
[X is an upper case this time]

 

Scroll to Top