Chris Wilson Posted February 22, 2008 Share Posted February 22, 2008 I want to write a script that will delete files in a directory that are duplicates. The duplicates have (for example) the name chevron[1].jpg or chevron[2].jpg the original being say chevron.jpg How can I write a single script to delete the [1] [2] [3] et cetera dupes? I can do any one by del *[1]*.jpg but not sure how to do the [2] [3] et cetera with one script and make it run contiguously. I have got this mess by carelessly adding the same files to a directory with an app that adds [1] to the first dupe [2] to the next and so on. Quote Link to comment Share on other sites More sharing options...
JustGav Posted February 22, 2008 Share Posted February 22, 2008 del *[?]*.jpg MAY work... * is a global sub, whereas ? is a character sub... I would however suggest testing it carefully first, because I'm not 100% sure what the first * might match... Quote Link to comment Share on other sites More sharing options...
cheekymonkey Posted February 22, 2008 Share Posted February 22, 2008 You might be able to test by doing dir *[?]*.jpg which will list the matching files. EDIT tested, and yes JustGav's method is fine Quote Link to comment Share on other sites More sharing options...
JustGav Posted February 22, 2008 Share Posted February 22, 2008 You might be able to test by doing dir *[?]*.jpg which will list the matching files. Yup, I'm not 100% sure because I'm having to think in regex and do a little conversion for i in [1 2 3 4 5 6 7 8 9 10] do rm -f cetron$i done But then I'm just geeky, Chris ignore the above snippit as it won't work in DOS Quote Link to comment Share on other sites More sharing options...
Schtuv Posted February 22, 2008 Share Posted February 22, 2008 * is difficult to use, as it'll look for anything before the '.'. '?' can be used to define a wildcard single space, but that leads to looong scripts if you can't predict the location of the '[1]'. I'm sure someone intelligent will be along soon to sort this. I may have a crack at it later :-) I suspect the easiest way is to export a list of files in a directory to a file, and pipe ' Then again, if you could sort by date to organize dupes. Quote Link to comment Share on other sites More sharing options...
Chris Wilson Posted February 22, 2008 Author Share Posted February 22, 2008 del *(*).jpg works, as the dupes all have the (*) just before the .jpg. Seems to be fine on a duplicate directory, so I'll risk it on the real thing now Cheers. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.