Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I have a folder with some contents (three files and a folder) that look like this:

-rwxr-xr-x 1 max max 14504 2011-05-31 16:55 main.css
-rwxr-xr-x 1 max max  2504 2011-05-31 16:55 reset.css
-rwxr-xr-x 1 max max   916 2011-05-31 16:55 scaffold.css
drwxrwxr-x 3 max max  4096 2011-05-31 16:55 ui-lightness

I want to add group write to all of them and remove the executable status for all users. I do the files first:

$ chmod g+w main.css reset.css scaffold.css 
$ chmod a-x main.css reset.css scaffold.css 
$ ls -l
total 28
-rw-rw-r-- 1 max max 14504 2011-05-31 16:55 main.css
-rw-rw-r-- 1 max max  2504 2011-05-31 16:55 reset.css
-rw-rw-r-- 1 max max   916 2011-05-31 16:55 scaffold.css
drwxrwxr-x 3 max max  4096 2011-05-31 16:55 ui-lightness

So far so good. Now, the ui-lightness folder already has group write, so i just want to remove exe status from it and all the files and subfolders inside.

$ ls -l ui-lightness/
total 40
drwxrwxr-x 2 max max  4096 2011-05-31 16:55 images
-rwxrwxr-x 1 max max 34146 2011-05-31 16:55 jquery-ui-1.8.11.custom.css

$ chmod -R a-x ui-lightness/
chmod: cannot access `ui-lightness/jquery-ui-1.8.11.custom.css': Permission denied
chmod: cannot access `ui-lightness/images': Permission denied

$ ls -l ui-lightness/
ls: cannot access ui-lightness/jquery-ui-1.8.11.custom.css: Permission denied
ls: cannot access ui-lightness/images: Permission denied
total 0
d????????? ? ? ? ?                ? images
-????????? ? ? ? ?                ? jquery-ui-1.8.11.custom.css
$ 

My first instinct is to panic a bit. But, adding the x status back on fixes it!

$ chmod -R a+x ui-lightness/

$ ls -l ui-lightness/
total 40
drwxrwxr-x 2 max max  4096 2011-05-31 16:55 images
-rwxrwxr-x 1 max max 34146 2011-05-31 16:55 jquery-ui-1.8.11.custom.css

Can anyone explain what's going on here? And how i remove the executable status without borking everything? This is in ubuntu 9.10 in case that's relevant.

cheers, max

share|improve this question
add comment

2 Answers

up vote 6 down vote accepted

The first parameter to your recursive chmod is the directory itself. You removed x bit on the directory, making it not searchable any more (that's what the x bit does for a directory). Then the chmod program could no longer search inside that directory and you get the permissions errors. try the following instead.

chmod -R a-x ui-lightness/*
share|improve this answer
    
ahhh - so directories need to have the x property then? I did not know that. Thanks! –  Max Williams Jun 7 '11 at 16:30
    
But, then, if i do chmod -R a-x ui-lightness/*, and there's another subdir inside that, then that subdir will get borked, right? –  Max Williams Jun 7 '11 at 16:31
4  
yes. You really should be using find . -type f -exec chmod -x {} \; to select only regular files. –  Keith Jun 7 '11 at 16:37
    
nice one, thanks. –  Max Williams Jun 9 '11 at 8:35
add comment

To remove executable status only for files, use:

$ chmod -R a-x+X ui-lightness/

The +X option is defined (see man chmod) to:

  • set execute permission only if the file is a directory or already has execute permission for some user

So, the lowercase -x option first removes the executable status from all files (and directories, of course), then the uppercase +X option sets it back only for directories.

It has one minor drawback: it sets the executable status to all subdirectories, even to those that didn't have it set prior to the chmod invocation. If that's not what you want, just use Keith's find solution.

share|improve this answer
    
this works perfectly with nested directories! –  Hugo Dozois Oct 24 '12 at 18:27
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.