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.

  • 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

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.


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

find . -name filename -exec chmod 755 {} \;
  • 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
  • 2
    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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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