Javatpoint Logo
Javatpoint Logo

File Permissions

All the three owners (user owner, group, others) in the Linux system have three types of permissions defined. Nine characters denotes the three types of permissions.

  1. Read (r) : The read permission allows you to open and read the content of a file. But you can't do any editing or modification in the file.
  2. Write (w) : The write permission allows you to edit, remove or rename a file. For instance, if a file is present in a directory, and write permission is set on the file but not on the directory, then you can edit the content of the file but can't remove, or rename it.
  3. Execute (x): In Unix type system, you can't run or execute a program unless execute permission is set.But in Windows, there is no such permission available.

Permissions are listed below:

permission
on a file
on a directory
r (read)
read file content (cat)
read directory content (ls)
w (write)
change file content (vi)
create file in directory (touch)
x (execute)
execute the file
enter the directory (cd)

Permission Set

Linux File Permissions1

Look at the above snapshot, there are ten characters (-rw-rw-r--) before the user owner. We'll describe these ten characters here.

File permissions for (-rw-rw-r--)

position characters ownership
1 - denotes file type
2-4 rw- permission for user
5-7 rw- permission for group
8-10 r-- permission for other

When you are the User owner, then the user owner permission applies to you. Other permissions are not relevant to you.

When you are the Group then the group permission applies to you. Other permissions are not relevant to you.

When you are the Other, then the other permission applies to you. User and group permissions are not relevant to you.

Permission Example

Now we'll show some examples how permissions can be seen for a file or directory.

Linux File Permissions2

Look at the above snapshot, different directories and files have different permissions.

First letter (-) or d represents the files and directories respectively.

Now, from remaining nine letters, first triplet represents the permission for user owner. Second triplet represents the permission for group owner. Third triplet represents the permission for other .

Setting Permissions With chmod

You can change the permissions with chmod command accordingly to your need. Below are some examples to change the permissions for different groups.

To add permissions to a group.

Syntax:

Example:

Linux File Permissions3

Look at the above snapshot, permission to execute is added to the user owner group.

To remove permissions from a group

Syntax:

Example:

Linux File Permissions4

Look at the above snapshot, permission to execute is removed from the group and permission to write is removed from the user owner.

To add permission to all the groups together

Syntax:

Example:

Linux File Permissions5

Look at the above snapshot, we have given permission to write for all the groups.

Note: Similarly, you can also remove the permission for all the groups.

To add permission to all the groups without typing a

Syntax:

Example:

Linux File Permissions6

Look at the above snapshot, this example is same as the earlier one only difference is that we haven't typed a in this.

To set explicit permission

Syntax:

Example:

Linux File Permissions7

Look at the above snapshot, we have set explicit permission to read and write for others.

To set explicit permissions for different groups

Syntax:

Example:

Linux File Permissions8

Look at the above snapshot, we have set permissions for all the three groups.


Setting Octal Permissions

Octal permissions can also be set for the groups.

For example, to set r octal will be 4, to set w octal will be 2, to set x octal will be 1.

Octal Table:

binary octal permissions
000 0 ---
001 1 --x
010 2 -w-
011 3 -wx
100
4 r--
101 5 r-x
110 6 rw-
111 7 rwx

From this we can conclude that,

and so on.

Linux File Permissions9

Look at the above snapshot, we have shown some random octal examples with the numbers 777, 274 and 111.


umask

While creating a file or directory, by default a set of permissions are applied. These default permissions are viewed by umask command.

For safety reasons all Unix systems doesn't provide execution permission to newly created files.

Adding execution permission is upto you.

Linux File Permissions10

mkdir -m

The 'mkdir -m' command can be used to set the mode.

Syntax:

Example:

Linux File Permissions11

Look at the above snapshot, we have created two files new1 and new2 with mode 777 and 000 respectively.

cp -p

The 'cp -p' command preserves the permissions and time stamps from source files.

Syntax:

Example:

Linux File Permissions12

Look at the above snapshot, earlier permissions for files list and dupli.txt were different. But after passing the command "cp -p list dupli.txt", both the files have same permissions.





Please Share

facebook twitter google plus pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA