How to change file and folders permissions with chmod – part 2

Previously, I have showed you how to change the file and folder access rights, by using the symbolic method. In this article I will show you how to change the file permissions in the octal mode (with numbers).

The chmod command is used for changing file and folder permissions.

In Linux and Unix, each file and folder permission: read, write, and execute has a value assigned, in order to make the access rights change easier:

  • read : 4
  • write : 2
  • exec : 1

Structured in the u g o format: user group others.

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

To find out the permissions, use ls -l or stat.

$ ls -l
total 0
-rw-rw-r-- 1 razvan razvan 0 2012-07-01 00:20 mike.txt
-rw-rw-r-- 1 razvan razvan 0 2012-07-01 00:23 mountains.JPG
-rw-rw-r-- 1 razvan razvan 0 2012-07-01 00:23 sea.JPG

$ 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.

rwx rwx rwx is 7 7 7 .

How to change the permissions with chmod:

Let’s set rwx (4+2+1) for the user, rw (4+2) for the group and r (4) for the others:

$ chmod 764 sea.JPG
$ stat -c "%a %n" sea.JPG
764 sea.JPG

Now I will set rw (4+2) for the owner, r (4) for the group and none (0) for the others:

$ chmod 640 sea.JPG
$ stat -c "%a %n" sea.JPG
$ 640 sea.JPG

The octal method is easier than the other one with letters. I use both this methods, depending on my needs.

If I want to set rwx,rw,r for example, I use chmod 764 filename, but if I need to add the exec permission for a script, I use chmod a+x filename.sh .

Share and Enjoy

  • Twitter
  • Facebook
  • Google Plus
  • Email
  • RSS
Tagged with: , , , , ,
Posted in The Linux and Unix Articles!

Leave a Reply

Your email address will not be published. Required fields are marked *

*