Understanding File and Folder Permissions (POSIX) on Linux and Mac OS X (using chmod)

(0 votes, average 0 out of 5)

Linux and Mac OS X both use a POSIX-compatible type of filesystem. This has to do with the way files are accessed by their owner and others. Files and folders have permissions for 3 different types of users:

  • Owner: this is the user who owns the file or folder
  • Group: this is the permissions for any user in the group that the file belongs to
  • Others: this is the permissions for all other users who don't fall into one of the two above categories

For each of these types of users, there are 3 different permissions which can be either granted or not granted, they are:

  • read (4) - gives that type of user the ability to read the file
  • write (2) - gives that type of user the ability to write to the file
  • execute (1) - gives that type of user the ability to execute the file as a program

Each of these types of permissions has an octal number associated with it (the number in parenthesis after it). To assign a file or folder more than one permission, add these numbers together. For example, to make a file both readable and writeable it would have the value 6 (4 + 2 = 6). Here's a list of all of the possible combinations:

 

0 --- no permission
1 --x execute 
2 -w- write 
3 -wx write and execute
4 r-- read
5 r-x read and execute
6 rw- read and write
7 rwx read, write and execute

 

 

The owner, group, and others each a digit to define permission for that type of user. For example a file or folder which has read, write, and execute permission by the owner, read and write permission by the group, and read permission by others would have to be 764 (rwxrw-r--). Similarly a file which is readable and writeable by the owner and group only would be 660 (rw-rw----). To view a file or folder's current permissions, use the ls command with the -l flag:

 

$ ls -l
-rw-r--r-- 1 user group      6644 2010-07-09 21:49 grep.py
drwxr-xr-x 3 user group      4096 2010-04-16 00:49 handbrake
-rw-r--r-- 1 user group      2773 2010-05-28 02:19 info.txt
-rw-r--r-- 1 user group      4542 2010-04-28 16:14 picture.png
 

 

 

To change the permissions of a file, use the chmod command followed by the octal permissions followed by the filename. For more information, see this article on common commands. Here is a brief example:

 

user@computer:~ $ ls -l winetricks
-rw-r--r-- 1 user group 161664 2010-05-26 19:44 winetricks
user@computer:~ $ chmod 775 winetricks
user@computer:~ $ ls -l winetricks
-rwxrwxr-x 1 user group 161664 2010-05-26 19:44 winetricks
 

 

To recursively change the permissions on a set of files and folders, use the -R flag: chmod -R 775 thedirectory/ .

 

Partner Links:
Last Updated on Saturday, 14 August 2010 19:48  
Related Articles

» Setting ownership (chown) on an NTFS partition in Ubuntu with NTFS-3G

If you want to set ownership of a folder on an NTFS drive in Linux, you need to change the ownership of its entry in /etc/fstab. Unfortunately you cannot use chown because NTFS is not a POSIX-compatible filesystem. Open up Users and Groups and find your user's ID. The first user on an Ubuntu install has uid 1000. Similarly, click on Manage Groups and find your group - the gid for the first user is 1000. Open /etc/fstab as root sudo gedit /etc/fstab and change the mount line to include the uid...

» Matplotlib IOError: Error While Writing to Output Stream

When using the Python Matplotlib library, Python may throw the following error:1IOError: error while writing to output streamCommonly this is caused because Python does not have access to the output stream -- in this case, usually some file that Matplotlib is attempting to write to.  This is typical when the Python script is being used on a web server.  For instance, the following code might not work if the directory "output" does not have write permissions on a...

» How to Use gprof (The Basics)

gprof is a GNU profiler, which allows users to see important information about their code such as how much time is spent in a function or how often certain call trees are executed.  It's a very powerful program -- this article will give you only the information you need to know to see a basic report.  Here's how to do it. 1.  Compile Your Code with the -pg flagCompile your C code as you typically would, but add in a -pg flag.  This indicates to the compiler that it should...