VegTo91
VegTo91

Reputation: 3

Unix administration: find command with -exec mv only moves certain amount of files

For some reason the command underneath only moves a certain amount of files and not all of them to the specified location:

find /directory1 -iname "*name1*" -or -iname "*name2*" -or -iname "*name3*" \
    -or -iname "*name4*" -exec mv -v {} /directory2  \;

What is the reason of the problem?

Upvotes: 0

Views: 64

Answers (1)

John Kugelman
John Kugelman

Reputation: 361987

You need to put parentheses around the -or conditions so that the -exec applies to all of them and not just the last one.

find /directory1 '(' -iname "*name1*" -or -iname "*name2*" -or -iname "*name3*" \
    -or -iname "*name4*" ')' -exec mv -v {} /directory2  \;

There are implied -ands between conditions and actions. When you write

find -cond1 -or -cond2 -or -cond3 -action

It's equivalent to

find -cond1 -or -cond2 -or -cond3 -and -action

Which due -and having higher precedence than -or, is equivalent to

find -cond1 -or -cond2 -or '(' -cond3 -and -action ')'

Upvotes: 3

Related Questions