Monday, May 3, 2010

File permissions vs Directory permissions



   Everything is a file in UNIX, they say. A file needs some necessary file permissions to be accessed, so is a directory. File permissions is one of the most common and important activity which every UNIX user comes across. However, directory permissions is not so common an activity and hence the assumption that the file permissions are the same as directory permissions, difference being applied on the directory level, which is incorrect. Lets see the difference between them.

The basic file/directory permission attributes are : r w x
r   -  read permission
w   -  write permission
x   -  Execute permission
File Permissions:

   Very quickly, lets see the file permissions with a file as example:
# ls -l file
-rw-r--r--   1 blogger        adm             27 May  2 08:04 file
#
 For simplicity, lets focus only on the owner permissions(highlighted in red). On the file named 'file' :

r   - Indicates the user can read the file.
w - Indicates the user can edit or delete the file.
'-'  - Indicates the user cannot execute the file since 'x' is not set.


Directory Permissions:

     Directory permissions are very different from the file permissions. Lets create a directory named 'abc' and check the differences. To understand the differences better,  all the file permissions are being removed and will be applied one by one. For simplicity, we deal only with the owner permissions:
#mkdir abc
#ls -l
total 0
drwxr-xr-x   2 blogger        adm             96 May  2 08:20 abc
#chmod -rwx abc
#ls -l
total 0
d---------   2 blogger        adm             96 May  2 08:26 abc
#
  1. Listing the files inside the directory:
#ls abc
abc unreadable
total 0
#
    It is the read permission of the directory which enables the unix user to list the files inside the directory.
#chmod u+r abc
#ls abc
#
   No files have been listed since the directory does not contain any. However, the point to be noted is no error is being thrown.

   2. Lets get into the directory 'abc':
#cd abc
abc: Permission denied.
#chmod u+x abc
#cd abc
#
   It is the executable permission on a directory which enables the user to get into the directory. However, the executable permission with respect to a file is completely different.

 3.   Lets now try to create a file under 'abc' directory:
#touch file
touch: file cannot create
   The user is not able to create a file in the abc directory because the directory does not have write permission.
#cd ..
#chmod u+w abc
#cd abc
#touch file
#
  The file got created successfully.  If creation is successful, so will be the deletion of the file. These are the differences between the permissions or attributes between file permissions and directory permissions.

2 comments:

  1. Great.very good explanations. I do appreciate.

    ReplyDelete
  2. Hi, I created a directory and put a file inside it and removed all the permissions. Even after giving just the read permission, the files in the directory are not getting listed.

    ReplyDelete