Trying to find these files and copy them to a directory (test_dir);


I would like to find all these files dynamically and copy them to a directory. I have tried the below command but it copies only last file(jenk*.zip) only, need a command which copies all the above files to a directory mentioned (test_dir);

find / -type f -name "*abc*.zip" -o -name "*kncb*.zip" -o -name "*jenk*.zip" ! -path '*/test_dir/*.jar' -exec cp -ar {} ./test_dir \;
  • are you guaranteed that there'll only be one of each of those? – Jeff Schaller Sep 25 '18 at 1:54
  • why are you recursively copying (-r) when you're looking (presumably) for single files? – Jeff Schaller Sep 25 '18 at 1:55

You need to apply ( ) around your OR construct.

An explanation can be found in the man page:

Please note that -a, when specified implicitly (for example by two tests appearing without an explicit operator between them) or explicitly, has higher precedence than -o. This means that find . -name afile -o -name bfile -print will never print afile.


touch afile bfile

Without ( ):

find . -name afile -o -name bfile -print

which has an implicit -a between -name bfile and -print:

find . -name afile -o -name bfile -a -print

With ( ) the result is as expected:

find . \( -name afile -o -name bfile \) -print

Your implicit -a lies between -o -name "*jenk*.zip" and ! -path '*/test_dir/*.jar'.

  • Getting an error when I run this from jenkins; find / (-name "abc.zip" -o -name "kncb.zip" -o -name "jenk.zip" ) ! -path '/test_dir/.jar' -exec cp -ar {} ./test_dir \; find: invalid expression; you have used a binary operator '-o' with nothing before it. – itgeek Sep 25 '18 at 13:57

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.