0
abc-1.0.zip
kncb-1.0.zip
jenk-1.1.zip

I want to find all the above files dynamically and copy them to a directory except jenk-1.1.zip. I have tried the below command but it also copies file(jenk*.zip), need a command which copies all the above files except jenk-1.1.zip to a directory mentioned (test_dir);

find . \( -name *abc*.zip-o -name *knc*.zip ! -name "*jenk*.zip"\)  ! -path '*/test_dir/*.jar' -exec cp -ar {} ./test_dir \;
  • if you're specifically matching "abc-1.0.zip" or "kncb-1.0.zip" why even bother excluding "jenk-1.1.zip"? It'll never match the preceding names... – Jeff Schaller Oct 15 '18 at 20:13
0
 -iname *.zip 

is looking for all zip files

! -iname "jen*.zip"

is ignoring all jenk* files

If you're looking for just zip files, there is no need to exclude jar files. If you search for files (find -type f) there is no need for the cp -r switch (recursive))

find . -type f \( -iname "*.zip" ! -iname "jen*.zip" \) -exec cp -av {} ~/test \;
  • That will copy all zip files except for the one starting with jenk. It's not a problem if those are the only three zip files in the entire directory including the subdirectories but if they aren't then it'll be too much. – Nasir Riley Oct 15 '18 at 20:21
0
cp $(find . -maxdepth 1 -name abc-1.0.zip -o -name kncb-1.0.zip) test_dir

Run that from the directory where the files reside. If you want to descend into subdirectories, remove maxdepth 1.

  • This looks correct, but might not work for abc-1.1.zip if OP works with versions. – Michael D. Oct 15 '18 at 20:32
  • He's not trying copy abc-1.1.zip. – Nasir Riley Oct 15 '18 at 20:47

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.