3

I try to filter some files I don't want to copy to another folder (a123*.zip in this case) and want to copy all other *.zip files

if [[ ! -f ./a123*.zip ]];  
then 
    # copy all files without a123*.zip 
fi

How do I trigger the copy?

nobody
  • 19,814
  • 17
  • 56
  • 77
Maikel
  • 47
  • 2
  • After some thought I decided this is not a dupe – the linked question is *specifically* about `ls`. Since it's not appropriate to use `ls` for programmatic solutions, the answer to that question cannot be appropriate to answer this question. Thus, I think it's not a dupe. – kojiro May 05 '14 at 19:27
  • Also http://stackoverflow.com/questions/216995/how-can-i-use-inverse-or-negative-wildcards-when-pattern-matching-in-a-unix-linu – devnull May 05 '14 at 19:30
  • @devnull Yep, that one is a much better candidate. I would change my close vote if I could. – Tom Fenech May 05 '14 at 19:32

2 Answers2

6

You can use extglob here:

shopt -s extglob
cp !(a123*.zip) /destination

If you want to copy all *.zip files except a123*.zip then use: (thanks @kojiro)

cp !(a123*).zip /destination
anubhava
  • 761,203
  • 64
  • 569
  • 643
  • This will match more than just `*.zip` files, though. I think you want `!(a123*).zip`, yes? – kojiro May 05 '14 at 19:23
  • But didn't OP write: `copy all files without a123*.zip` but yes if only `*.zip` are to be copied then `!(a123*).zip` is the right glob pattern. – anubhava May 05 '14 at 19:24
  • Good to know it worked for you but I won't categorize it as `"dirty"` solution :) – anubhava May 05 '14 at 19:37
2
find . -maxdepth 1 -type f -and name \*.zip -not -name a123\*.zip -exec cp "{}" "$destination"
R Sahu
  • 204,454
  • 14
  • 159
  • 270
  • 4
    Please don't [parse `ls`](http://mywiki.wooledge.org/ParsingLs) – kojiro May 05 '14 at 19:24
  • @kojiro Thanks for the link. It makes sense. I updated my answer to use a better method to pick the files. – R Sahu May 05 '14 at 19:40
  • 3
    But `find` isn't much better, as it says in the same article. At very least, you need to use the `-print0` flag or otherwise carefully control the delimiter. – kojiro May 05 '14 at 19:42
  • Hey, in your update 2 you need to quote those wildcards (and `$destination` too)! do you know what the shell does with wildcards??? – gniourf_gniourf May 05 '14 at 20:22
  • @gniourf_gniourf Yes, I am aware of what shell does with wild cards. For some reason, in my testing I didn't see any difference. I agree that it's safer to escape the wild cards. – R Sahu May 05 '14 at 20:27
  • @gniourf_gniourf I know why it didn't make any difference in my testing. There was nothing that matched the wild card in the directory I was running `find`. – R Sahu May 05 '14 at 20:33
  • Just remove the whole suggestion to parse ls, because that's Bash pitfall #1 http://mywiki.wooledge.org/BashPitfalls#for_i_in_.24.28ls_.2A.mp3.29 – Aleks-Daniel Jakimenko-A. Jul 02 '14 at 06:38