I've this command in a .sh file

sudo find $PROJECT_ROOT -type f \
     -exec sudo chmod ugo-x '{}' \; \
     -exec sudo chmod go-w '{}' \; 

As you can see, foreach file I need to remove write permission for group and other, leaving 'as is' the eventual write permission for the user.

AND remove execute permission for every one.

I ask you if there is a way to optimize this... I've not a lot of experience of chmod but this is time consuming: 4-5 minute per projecct folder, for each of our 41 actual projects and this must be executed 2 times per week. (Please do not ask WHY, it's not important, I must DO it)


In addition to @Sisir's answer, the first thing you can do is use "+" instead of ";" in the find command. This makes it run one single command on all matching files instead of one command per file:

sudo find $PROJECT_ROOT -type f -exec chmod a-x,go-w '{}' +

If the projects contain too many files (there are limits on command line length) you can still use one single chmod per directory with a small additional script:

#! /bin/bash
# chmod's all files in the $1 directory
find "$1" -maxdepth 1 -type f -exec chmod a-x,go-w {} +

and then:

sudo find $PROJECT_ROOT -type d -exec chmod_script {} \;

(btw, I don't think you need a sudo on the command executed by find, it should normally inherit the userid of find?)

  • Thank you, I didn't know I could combine multiple variations of permissions in a single CHMOD command.I'm trying. I'll reply you soon with esits – realtebo Sep 15 '17 at 12:58
  • Wow ! Totale mid time per project is now 52% of the previous. Thanks ! Also, I confirm we don't need "sudo over sudo". – realtebo Sep 15 '17 at 13:04

Simplify the command as follows and then check if it improves the performance:

sudo find $PROJECT_ROOT -type f -exec sudo chmod a-x,go-w '{}' \; 

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.