I have a couple of files which follow this syntax:

  • *_yyyy-mm-dd_hhhmmm.*

Example:

  • *_2010-01-01_00h10m.*
  • *_2010-01-01_01h10m.*
  • *_2010-01-01_02h10m.*
  • *_2010-01-01_03h10m.*
  • ...
  • *_2010-01-01_23h10m.*
  • *_2010-02-01_00h10m.*
  • *_2010-02-01_01h10m.*
  • *_2010-02-01_02h10m.*
  • ...
  • *_2010-02-01_23h10m.*
  • ...
  • *_2010-12-01_23h10m.*

I would like to keep only this files

  • *_2010-01-01_00h10m.*
  • *_2010-02-01_00h10m.*
  • *_2010-03-01_00h10m.*
  • ...
  • *_2010-12-01_00h10m.*

and delete the others. or maybe move them to a subdirectory to be safe...

Does someone has a smart on-liner for this challenge?

Thanks, Udo

link|improve this question

similar superuser.com/questions/231718/… – akira Jan 23 '11 at 19:27
feedback

2 Answers

up vote 2 down vote accepted

In bash:

shopt -s extglob
rm !(*_2010-??-01_00h10m.*)
link|improve this answer
feedback

very naive way

mkdir keep_these
mkdir remove_these
mv *_00h10m* keep_these
mv *.* remove_these

or a oneliner

ls * | grep -v "_00h10m" | while read a; do mv $a /tmp; done
link|improve this answer
naive indeed :) -> but good -> do you also have a solution without the need of subdirs? – udo Jan 23 '11 at 18:36
1  
@udo - Yes just replace mv $a /tmp with `\rm $a´ in the last example – Nifle Jan 23 '11 at 18:52
feedback

Your Answer

 
or
required, but never shown

Not the answer you're looking for? Browse other questions tagged or ask your own question.