File permissions in linux shell

A file or directory can have below types of permission attributes.
  • Read (r)
  • Write (w)
  • Execute(e)
A file also belongs to specific owner and group. For example – In below image, we have listed down the permissions of file “a.sh”. As you can see permissions are displayed in below format. rwxrwxrwx Also note that paul is the owner of this file and group of this file is paul as welll. You might be wondering why we have 3 sets of permission (rwx-rwx-rwx) for same file. You should remember that First set of permissions are for the user (u) of the file. Second set of permissions are for the users in specific group (g) of the file. Third set of permissions are for all other users (o).Now consider permissions for below file – abc.txt. You can notice that owner and group of this file has only read and write permission. While all other users have only read permission. So if any other user tries to write to this file or execute this file, he will encounter error saying permission denied.We can use 3 commands to manage the permissions of the file.
  • chmod – change the file permissions
  • chown – change the ownership of the file
  • chgrp – change the group of the file

chmod command in Linux

chmod command is used to change the permission of a file or directory. There are 2 ways in which we can specify the permission attributes.
  • symbolic
  • numeric

Symbolic file permission

It is very easy to work with symbolic file permissions. Let us say you want to give write permission on abc.txt file to others(o), then you can use below syntax. Here o stands for others. + means we are adding permission. w means we are adding write permission.
 
chmod o+w abc.txt

If you want to remove write access from others, you can use below syntax. here – means we are removing permission.
 
chmod o-w abc.txt

Now let us say you want to add write permission for user of the file and at the same time remove execute permission for group. Then you can execute below command.
 
chmod u+w,g-x abc.txt

Numerical permissions of file

You can also use numerical representation in chmod command. Below
Number Permission
7 rwx
6 rw-
5 r-x
4 r–
3 -wx
2 -w-
1 –x
0 —
For example to give read/write/execute permission to user of the file and no permission to group and other users, you can use below syntax.
 
chmod 700 abc.txt

chown command in Linux

chown command is used to change the owner and group of a file. Here is the syntax of chown command
 
chown <new_user>:<new_group> file

For example – To change owner to paul and group to dev for file “abc.txt”, you can use below command.
 
chown paul:dev abc.txt

To change the owner and group of all files in a directory, you can use -R option.
 
chown -R paul:dev /home/project

chgrp command in Linux

chgrp command is used to change the group of a file. Here is the syntax of chgrp command.

chgrp <group_name> file

You can also change the group of files recursively using below syntax.
 
chgrp -R <group_name> directory

Note that you might need to execute the commands using sudo to avoid error saying operation not permitted.

Web development and Automation testing

solutions delivered!!