Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Count the number of subdirectories without the execute bit

How can I count the number of subdirectories that don’t have the execute bit set?

This is my attempt but is there a better or more elegant way?

count=0; for d in */; do [[ -d $d && ! -x $d ]] && (( ++count )); done
printf %s\\n "$count"

My main interest is in checking if the execute bit is not set for all. That is not just for the current user.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

The following counts directories without executable bit for current user:

find . -mindepth 1 -maxdepth 1 -type d '!' -executable -printf . | wc -c

The following counts directories that have 0 executable bits:

find . -mindepth 1 -maxdepth 1 -type d '!' -perm /111 -printf . | wc -c

The following counts directories that have 2 or less executable bits set:

find . -mindepth 1 -maxdepth 1 -type d '!' -perm -111 -printf . | wc -c

Parts from man find:

   -perm /mode                                                                                                                         
          Any  of  the permission bits mode are set for the file.

  -perm -mode
          All  of the permission bits mode are set for the file.

   -executable                                                                                                                         
          Matches files which are executable and directories which are
          searchable (in a file name resolution sense) by the  current
          user.
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading