starscream_disco_party
starscream_disco_party

Reputation: 3006

find commands not executing as intended

I want to find all files with the filename mask pattern of filemask1 or filemask2, and to exclude the unwanted* patterns, that are contained in the specified directories. I'm trying to run the following:

find dir/ other_dir/ parent/child/dir/ parent/child/other_dir/ parent/other_dir/ -iname '*filemask1*' -o -iname '*filemask2*' ! -iname '*unwatned1*' ! -iname '*unwanted2*' ! -iname '*unwanted3*' ! -iname '*unwanted4*'  ! -iname '*unwanted5*' ! -iname '*unwanted6*' ! -iname '*.xls*'

I'm executing this from a directory that contains each of the paths that I'm trying to search. The above gives me all of the files in the specified directories without excluding the unwanted patterns.

find dir/ other_dir/ parent/child/dir/ parent/child/other_dir/ parent/other_dir/ \(-iname '*filemask1*' -o -iname '*filemask2*'\) ! -iname '*unwatned1*' ! -iname '*unwanted2*' ! -iname '*unwanted3*' ! -iname '*unwanted4*'  ! -iname '*unwanted5*' ! -iname '*unwanted6*' ! -iname '*.xls*'

gives me:

find: invalid expression; you have used a binary operator '-o' with nothing before it.

While

find dir/ other_dir/ parent/child/dir/ parent/child/other_dir/ parent/other_dir/ ! -iname '*unwatned1*' ! -iname '*unwanted2*' ! -iname '*unwanted3*' ! -iname '*unwanted4*'  ! -iname '*unwanted5*' ! -iname '*unwanted6*' ! -iname '*.xls*' \(-iname '*filemask1*' -o -iname '*filemask2*'\)

gives me

find: paths must precede expression: (-iname
Usage: find [-H] [-L] [-P] [-Olevel] [-D help|tree|search|stat|rates|opt|exec] [path...] [expression]

Am I missing something regarding how this is getting interpreted? Is there a way to simplify this so it's not as verbose? I'm aware that having better filenames would alleviate a lot of the issues, but I am not in control of that :/

A bonus would be if anyone could include a way to prune multiple patterns of directories that would be contained in the directories specified to search in

Upvotes: 1

Views: 1296

Answers (1)

WillyS
WillyS

Reputation: 36

if I understand you the answer for your first question is to group wanted and unwanted names separately.

find \
dir/ other_dir/ parent/child/dir/ parent/child/other_dir/ parent/other_dir/ \
\( -iname '*filemask1*' -o -iname '*filemask2*' \) \
! \( -iname '*unwatned1*' -o -iname '*unwanted2*' -o -iname '*unwanted3*' -o -iname '*unwanted4*' -o -iname '*unwanted5*' -o -iname '*unwanted6*' -o -iname '*.xls*' \)

Also the unwanted dir can be excluded by simple regexp.

Upvotes: 2

Related Questions