1

I need to get the number of directories that

  • Can only be read and opened by the owner of the directory.
  • Can only be opened by their group.

My current solution is this:

ls -la . | grep "^dr-x--x---" | wc -l

Is this correct? Or is there a more proper way to do this?

Jeff Schaller
  • 67,283
  • 35
  • 116
  • 255
Ge To
  • 149
  • 6
  • The proper way is using find - check the -type, -user and -perm flags that allow specifying type (e.g. directory vs. file), owner and permissions, respectively. Please have a good read regarding how to use -perm as there are ways to find e.g. partial and exact permission matches. find will descend into directories, so see if you need the -maxdepth flag, too. – FelixJN Mar 16 '21 at 13:03
  • But my solution should be working too, right? – Ge To Mar 16 '21 at 13:06
  • It would e.g. fail with a file containing a literal newline and your search string filenameNEWLINEdr-x--x--- - very constructed, but it shows the limitations. find would allow printing one character per result and skip these problems. In general the approach could work, but as said find would be the "proper" way you requested. Also your way does not allow optional read access - the question is not 100% clear here, if that might be allowed. – FelixJN Mar 16 '21 at 13:17
  • 1
    If the code in https://unix.stackexchange.com/q/128985/117549 looks like fun to you, then yes, but the output of ls wasn't meant to be parsed by computers. – Jeff Schaller Mar 16 '21 at 13:21

0 Answers0