6.6 Changing permissions

Permissions can be changed with the chmod command.

To add or remove a permission, use + or -, followed by the permission we would like to change (r, w or x).

For example, let’s add the executing permission to all owners:

$ chmod +x test.sh
$ ls -l test.sh
-rwxr-x---. 1 andreas andreas 31 Sep 26 15:14 test.sh

… and remove it again:

$ chmod -x test.sh
$ ls -l test.sh
-rw-r-----. 1 andreas andreas 31 Sep 26 15:14 test.sh

To change permissions of only one owner, add the owner in front (u for user, g for group, o for other).

For example, let’s remove read permission from other:

$ chmod o-r test.sh
$ ls -l test.sh
-rw-r-----. 1 andreas andreas 31 Sep 26 15:14 test.sh

and remove write permissions from group:

$ chmod g-w test.sh
$ ls -l test.sh
-rw-r-----. 1 andreas andreas 31 Sep 26 15:14 test.sh

Back to our original problem: How can we run test.sh? To run a script, the user (me) needs to have executing rights. Currently, we do not have this, but we can easily add it with:

$ chmod u+x test.sh
$ ls -l test.sh
-rwxr-----. 1 andreas andreas 31 Sep 26 15:14 test.sh

Now, let’s run the script!

$ ./test.sh
Hello world