problem with chmod command

I have a directory structure in which I'd like to change the permissions on all header files. I tried this command:

chmod -R 644 *.h

but it just changed the files in the current directory. Isn't the "-R" supposed to make the command apply to subdirectories as well?

Thanks.
find . -name '*.h' | xargs chmod 644
Thanks for that alternative. But I'm still curious as to why the way I tried it didn't work.
Since you chose to use only the *.h files, no directories were included in your command, so there are no directories to apply recursion.
Also, if your files contain spaces, that won't work. I think the best way would be

find . -name '*.h' | while read file
do
	chmod 644 $file # edit
done
Last edited on
Mmm, that doesn't seem to do the job on. On my box,
 
find ~ | while read file; do pwd; done

prints /tmp lots of times.

To deal with spaces, I use:
 
find . -name '*.h'  | sed s/^/\"/g | sed s/$/\"/g | xargs chmod 644 


I hope someone can post an impovement as I find it onerous.
That's because each line outputted by find is read by while and assigned to the variable file. If pwd accepted arguments, you would do

find ~ | while read file; do pwd $file; done
Yes, my mistake.

So shouldn't your example be:
 
find .  -name '*.h' | while read file; do chmod 644 $file; done
Last edited on
Ops...
find . -name *.h -execdir chmod 664 '{}' \;
Last edited on
After all that, it turns out the example still doesn't work. It needs to be:
 
find .  -name '*.h' | while read file; do chmod 644 "$file"; done
Topic archived. No new replies allowed.