chmod: removing “x” permission recursively totally borks the permissions

Question

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

Asked by Max Williams

Answer

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/*
Answered by Keith