Recent

Changing File permissions(Read,write,execute) in Linux

What is File permission

chmod command is used to change the permissions on files and directories in Linux. But before using chmod command we need to understand few things.

In Linux every file and directory is associated with an owner/user , group and other/world. Every file and folder has a set of permission flags read,write and execute for users/owner , group and other. chmod modifies the permissions for users , groups and others.

type the following command:
ls -l command shows the permissions and associated group for files in linux:

root@seven:~/Desktop# ls -l
total 16
-rwx--x--x 1 root root   28 Oct 28 03:57 gmail
drwxr-xr-x 2 root root 4096 Oct 28 03:52 python
-rw-r--r-- 1 root root  194 Oct 28 03:51 textfile
-rw-r--rw- 1 root root   65 Oct 28 03:55 users

- : permission flag is not set.
r : File is readable.
w : File is writeable.
x : File is executable.

The above fields represent:
1 : Set of 10 permission strings(-rwx--x--x)
2 : link count.
3 : Owner of the file..
4 : File associated with group.
5 : Size of the file.
6 : Date of last modified.
7 : Name of the file.

When you do a listing first thing you see that , permission strings are broken up into four groups and 10 strings.
*Note-permission strings sequence goes from left to right.

First Group 1: The first group is only 1 character if it's a - then it's a regular file.if it's a d then it's a directory.

Second Group 2 3 4 : The second group rwx represents the read,write and execute permissions for owner/user of the file.

Third Group 5 6 7 : The Third group rwx represents the read,write and execute permissions for group of the file.

fourth Group 8 9 10 : The fourth group rwx represents the read,write and execute permissions for other/world.


Give Read Permission to owner/user of the file.

Syntax: chmod u [user] + [plus sign when you are giving permission] r [Read permission] filename
root@seven:~# chmod u+r textfile

Take Read Permission away from owner/user of the file.

Syntax: chmod u [user] - [minus sign when you are taking away permission] r [Read permission] filename
root@seven:~# chmod u-r textfile

Give write permission to owner/user of the file.

Syntax remains the same. Just replace r with w because we are giving write permission.
root@seven:~# chmod u+w textfile 

Take Write Permission away from owner/user of the file.

In the same way you can take away write permission by adding - between u and w.
root@seven:~# chmod u-w textfile

Make file executable or Give execute permission

It is important you will be doing this quite often in linux. Like when you write bash scripts to automate linux then you have make them executable.
Syntax remains the same Just add x after the plus sign.
root@seven:~# chmod u+x textfile 

Take execute permission away

Just add - between u and x.- sign is used to take permissions away.
root@seven:~# chmod u-x textfile 
Tip: You can give or take away permissions in a single statement.
  root@seven:~# chmod u+rwx textfile 

Changing File permissions for Groups

Everything remains the same just add g in the place u because g represents group and u users or owner of the file.

Give read permission to the group

root@seven:~#  chmod g+r textfile

Give write permission to the group

root@seven:~#  chmod g+w textfile

Give execute permission to the group

root@seven:~#  chmod g+x textfile

Take away read , write and execute from group

root@seven:~#  root@seven:~# chmod g-rwx textfile 
All the 3 permissions will be taken away.You can do it one by one too but it saves time.

Give read , write and execute permissions to other also known as world.

root@seven:~#  root@seven:~# chmod o+rwx textfile 
All the 3 permissions will be given to the other or world.

Take away Permissions from world/other.

root@seven:~#  root@seven:~# chmod o-rwx textfile 

Just give the command that you want to unalias along with unalias command.

That's it guys use your imagination and create some useful aliases.