1

I present you the following use-case:

[root@localCentOS71 folder]# ls -lah
total 16K
drwxr-xr-x 3 root root  4.0K Dec 22 08:52 .
drwxr-xr-x 4 root root  4.0K Dec 21 14:59 ..
-rw-r--r-- 1 root test     0 Dec 22 08:52 file
drwxr-xr-x 2 root root  4.0K Dec 21 14:59 hi
-rw-r--r-- 1 root root     0 Dec 22 08:46 .htaccess
-rw-r--r-- 1 root root   175 Dec 22 08:47 test

I am trying to invoke a find command, to perform:

  • Find only regular files
  • Find where owner is not root or
  • Find where group is not root or
  • Find where permissions are not 775 and
  • Exclude .htaccess files

Current command:

find /folder -not -user root -or -not -group test -type f \( ! -iname ".htaccess" \) -or -not -perm 775

Desired output:

/folder/test
/folder/file

Actual output:

/folder
/folder/.htaccess
/folder/hi
/folder/test
/folder/file
  • find always finds only files. Do you mean that you want it to find only regular files (as opposed to files of type directory or symlink or device...)? – Stéphane Chazelas Dec 22 '16 at 9:17
  • yes, I meant regular files. Therefore the argument -type f is given. – Kevin Dec 22 '16 at 9:22
3

Beware that -a (implicit between two predicates if ommited) has precedence over -o, so you need to use parentheses:

find /folder ! -name .htaccess -type f \( \
   ! -user root -o ! -group test -o ! -perm 775 \)

Or:

find /folder ! -name .htaccess -type f ! \( \
   -user root -group test -perm 775 \)

I do the -name first as an optimisation as it doesn't need to do a lstat() on the file. Some find implementations do that optimisation by themselves (re-order the list of predicates internally).

Note that -not and -or are non-standard GNU extensions. ! and -o are the standard equivalent.

Because of the precedence rules, your

find /folder -not -user root -or -not -group test -type f \( 
  ! -iname ".htaccess" \) -or -not -perm 775

Is actually interpreted as:

find /folder \( -not -user root \) -or \
             \( -not -group test -a \
                -type f -a \
               \( ! -iname ".htaccess" \) \
             \) -or \
             \( -not -perm 775 \)
  • Yes, this works. #win – Kevin Dec 22 '16 at 9:45

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.