Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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

share|improve this question
    
similar superuser.com/questions/231718/… –  akira Jan 23 '11 at 19:27

2 Answers 2

up vote 2 down vote accepted

In bash:

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

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
share|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

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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