Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

I need to know how to apply a CHMOD command to only files that have a specific name recursively?

chmod 755 -R filename

Something like above but it should apply to any filename that exists in any sub folders.

share|improve this question
    
do you want to apply this to a filename or a file type ie .html, .php etc –  seanl Jan 18 '10 at 16:32
    
Just a file with the name filename –  Camsoft Jan 18 '10 at 16:34

3 Answers 3

up vote 11 down vote accepted

This will find all files named filename, recursively from the current directory, and pass them to chmod.

find -name filename | xargs chmod 755

See the manpages for find and xargs for more advanced options. In particular, look at the -print0 flag and corresponding -0 to xargs if you get errors about files with spaces in the name.

share|improve this answer

There's also a exec parameter in find that will do the same:

find . -name filename -exec chmod 755 {} \;
share|improve this answer
    
The trade off here is execution efficiency. Xargs will only call chmod approximately the minimum number of times necessary for the number of files, the exec option to find will call it once for each file found. –  Scott Pack Jan 18 '10 at 17:47
1  
However, if your version of find supports it, you can use a + instead of the \; to get xargs-like functionality. –  Dennis Williamson Jan 18 '10 at 18:46

You can get the best of both worlds if you use the + operator to find to make it run chmod on many files at once.

find . -name filename -exec chmod 755 '{}' +

You should always put a ' around your {}, otherwise a filename with a space in it could mess things up. In this case it's not a problem, but I have a habit of doing it.

share|improve this answer

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.