If you run the ll
command, the result will be like
drwxrw-r-- 3 name name 4096 Sep 10 22:30 happy
The leading ten characters specify the type and permission information of the file. The leading d
means happy
is a directory and the remaining nine says that the owner of the file (the rwx
part) can read, write and execute the directory, members of the group of the file (the rw-
part) can read and write the directory, and anyone else (the r--
part) can only read the directory.
Wait a minute. What do you mean by “write and execute the directory”? For directories, “read” means you can search the directory, like ls
, “write” means you can create, edit or delete files inside the directory and “execute” means you can enter the directory.
To change the permission mode of a file for each of the three kinds of users, you have two ways to do it.
The first one is more straightforward, like
chmod u=wr,og=r file
u=wr
consists of three parts: who, what and which.
The who part can be one of the four kinds of users, that is, all users (a), the owner of the file (u), members of the group of the file (g), and others (o).
The what part says what you want to do, granting permission (+), revoking permission (-) and setting permission mode (=, which means a qualified user will have all specified permissions and specified permissions only).
The which part say which of the three kinds of permissions you want to specify, that is, read, write and execute.
The other option is to use numerical shorthand to describe the permission mode of a user, like
r w x
0 0 1 1
0 1 0 2
1 0 0 4
# So...
r w x
1 1 1 7r w -
1 1 0 6r - x
1 0 1 5
So let’s say you want the owner of the file to have all three permissions, the numerical shorthand for this permission mode will be 7; if it’s only write and read permissions, the numerical shorthand will be 6, etc. So you can guess what chmod 766 file
means.
If you want to apply the permission mode also to all the files within a directory, you can use the -R
flag to make it recursive.
More on this can be found here.