0

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

udo
  • 8,001
  • 7
  • 38
  • 47
  • similar http://superuser.com/questions/231718/remove-all-files-except-for-a-few-from-a-folder-in-unix – akira Jan 23 '11 at 19:27

2 Answers2

2

In bash:

shopt -s extglob
rm !(*_2010-??-01_00h10m.*)
Ignacio Vazquez-Abrams
  • 111,361
  • 10
  • 201
  • 247
1

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
Nifle
  • 34,203
  • 26
  • 108
  • 137