Change File Permissions using Terminal in Mac OS X

Filed under Uncategorized on Wednesday, 4 July, 2007 9:56 pm

Terminal

Changing file permissions on the Mac is something you need to know how to do if you want to create your own non-home directory only shares on the Mac. Leopard implements folder sharing similarly to the way SharePoints does. I find myself needing to change permissions regularly because certain programs like to change file permissions on their own. The Terminal is generally better for changing permissions because it is much faster and also because you have more control. Sometimes you may need to do this is the file permissions get messed up in Leopard's File Sharing under System Preferences.
Here's how it works:


* First, open the Terminal, then use the command 'cd'. For example, if you wanted to change to your Desktop directory, you would type:

cd ~/Desktop

to change to your desired directory.  Note that the tilde '~' represents your home directory.
The command 'ls' then shows the files within the current directory. You can also use:

ls -la

The 'l' switch lists files in long format, and the 'a' switch lists all files, including hidden ones.
If you are the owner of a file, you have the ability to change its permissions with the command:

chmod

There are several ways chmod can be used, however I will only focus on the octal system. The first number represents the owner's permission, the 2nd represents the groups permission, and the last one represents the world's or everyone's access. The permission is calculated using simple arithmetic.

The first number represents the access granted to the file's owner. The next number, represent access granted to the file's group owner, and the last number represents everyone else's level of access. The possibilities for permissions include: read (4), write (2), execute (1), and no permission (0).

The permission is set using the chmod command:

chmod 755 filename

However a more practical use of the command would be:

sudo chmod -R 755 directoryname

This command will recursively change the permissions on all files and folders contained within that directory because we used -R.

Okay, so just to rehash again:
The 7 comes from 1 + 2 + 4. This means read, write and execute permission (for the owner).
The 5 comes from 1 + 4. This means execute and read permission (for the group).
The 5 comes from 1 + 4. This means execute and read permission (for everyone).

This means that the file's owner has read, write, and execute permission, but group and world only have read and execute permissions. Managing file permissions on the Mac isn't that difficult once you get the hang of it. And doinging it youself if more powerful that using Disk Utiity, however you should take care as you can screw things up when using the sudo (root permission) command.

4 Comments »

Comment by plgx

February 3, 2009 @ 12:02am

Thanks for publishing tihs tutorial. I had tried doing this, but used the -r for recoursive. I got:

> chmod: 755: No such file or directory

I had to go around doing first a sudo chown +R /pathtofile, then a cd to the files parent and then chmod +r to the file.

I imagine that with the +R the terminal would've accepted the chmod 755 command.

Comment by Kasper

March 20, 2009 @ 3:26am

It worked perfect for me, i just used:

1. cd ~/Desktop
2. Hit enter
3. sudo chmod -R 755
4. drag the folder into the terminal
5. hit enter

Should do it if you by a mistake locked a folder or changed the permission to everyone: no access

Comment by Brock

March 20, 2009 @ 2:17pm

@plgx

> chmod: 755: No such file or directory

My mistake. I shouldn't have had the / infront of the directory name in there.

sudo chmod -R 755 test

will work fine...updating this in the article :)

Comment by Brock

March 20, 2009 @ 2:30pm

I would also like to point out to everyone that you probably do not want to be using +R, you should get using -R unless you need it.

+R will APPEND your permissions to a file/directory. Leaving others permissions intact
-R will change the permission to a file/directory for your user name and CLEAR everyone elses.

Both will work but please be aware of the security issues. You don't want others having access to your files.

RSS feed for comments on this post. TrackBack URI

Leave a comment

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>