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.

9 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.

Comment by mike

August 16, 2009 @ 6:49am

thanks. this is currently saving my life.

Comment by Roy

September 4, 2009 @ 4:44am

re: Comment 9429

chmod -R means execute the chmod recursively through all enclosed folders
chmod -r means remove the ability to read for all (user, group, other) on whatever file is specified
chmod +r means add the ability to read for all (user, group, other) on whatever file is specified
chmod +R is not valid syntax

from the Terminal application, things become (mostly) case sensitive, as in most Unix systems. For more information on the correct syntax for chmod, type 'man chmod' (without the single quotes) from the command line and read the man page.

re: Comment 9424
changing permissions will *not* work on a file that has been locked. First one must unlock the file, then change the permissions. Files attributes, such as locked, invisible, etc., can be manipulated by the /usr/bin/SetFile command.

Comment by jeff

December 16, 2009 @ 6:32am

what it i need to do a specific file to make it writable to internet. i am being told to use chmod 0777 on file. this in my site directory 3 deep in a folder? how do i get to the folder and what is the command exactly?

Comment by Fabricio Dore

December 23, 2009 @ 8:17pm

I went through the same issue because I accidentally changed the permissions of my system. Following is the step by step of how I solved my problem (I have a mid-level of programming savvyness):

+++
Step by step:

(1) Start the computer on single user mode:
cmd s

(2) Do the FSCK thing (type exactly the following and press enter/return):
/sbin/fsck -fy

If fsck displays the following message you are on the right way:
***** FILE SYSTEM WAS MODIFIED *****

(3) Mount the disk (the -uw is critical // type exactly the following and press enter/return):
/sbin/mount -uw/

(4) Change the permissions (type exactly the following and press enter/return):
chmod -R 755 /

(5) Boot (you will have to type it twice)
exit
*****Gives you a message*****

exit

(6) The system should start up fine. If not, force restart with the on/off button.

(7) Once back in the GUI or Graphic Interface, open DiskUtility from the Utilities folder inside Applications. Run the option Verify Permissions after selecting your harddrive from the menu on the left. After it finished finding problems everywhere, select Repair Permissions.

(8) Done!

+++
Other Mac OS boot options:

To use these boot shortcuts or commands, hold down the bolded keypress while your system is booting up. If it does not work, you probably did not start holding down the key (or key combination) soon enough.

shift – Safe Mode

Booting with the shift key depressed starts OS X in Safe Mode with Account preferences ignored and unessential kernal extensions / kexts disabled.

option – Startup Manager

Booting with the option key depressed starts OS X with the Startup Manager which allows the user to select which OS or partition from which to boot.

Mouse button – Eject Media

When the user boots with the mouse key depressed, any media in the drive such as a DVD or CD is ejected.

cmd v – Verbose Boot

If this key command is held down when booting, all the startup messages that are typically hidden will be displayed. This is an excellent method for debugging booting issues.

c – Boot from CD/DVD

Booting with the “C” key held will tell the system to boot directly from a CD/DVD in the drive.

t – Target Mode

If the “T” key is held during the bootup, the computer will be placed in target mode. This basically allows the computer to be accessed by other systems as a FireWire drive. Other computers can read, write, or mount the target mode system just like any other FireWire drive.

Other Bootup Shortcuts —

cmd s – Single User Mode

x – OS X Force boot

r – PowerBook Display Reset

n – Boot up from NetBoot Server

+++
More

Bypass startup drive and boot from external (or CD).... CMD-OPT-SHIFT-DELETE
Boot from CD (Most late model Apples) ................. C
Boot from a specific SCSI ID #.(#=SCSI ID number)...... CMD-OPT-SHIFT-DELETE-#
Zap PRAM .............................................. CMD-OPT-P-R
Disable Extensions .................................... SHIFT
Rebuild Desktop ....................................... CMD-OPT
Close finder windows.(hold just before finder starts).. OPT
Boot with Virtual Memory off........................... CMD
Trigger extension manager at boot-up................... SPACE
Force Quadra av machines to use TV as a monitor........ CMD-OPT-T-V
Boot from ROM (Mac Classic only)....................... CMD-OPT-X-O
Force PowerBooks to reset the screen................... R
Force an AV monitor to be recognized as one............ CMD-OPT-A-V
Eject Boot Floppy...................................... Hold Down Mouse Button
Select volume to start from............................ OPT
Start in Firewire drive mode........................... T
Startup in OSX if OS9 and OXS in boot partition........ X or CMD-X
Hold down until the 2nd chime, will boot into 9?....... CMD-OPT
OSX: Watch the status of the system load............... CMD-V
OSX: Enter single-user mode (shell-level mode)......... CMD-S

Comment by Beck

January 6, 2010 @ 1:15am

How do you set permission for another user on a file?

Ex: file.txt has userA as owner.
I would like to let staff and everyone permissions as they are, i.e. read only. But I want to add read and write permissions for userB.
It's like making a chmod 755 for userA, then another chmod 755 for userB, but all that with a terminal command in userA's session.

Thanks for your help.

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>

:) :( :o :? 8) :D :x :P :wink: