From: "Brian D. Burns" Date: 2013-01-10T18:22:38+09:00 Subject: Re: One liner for filenames On 01/08/2013 05:04 PM, Dami叩n M. Gonz叩lez wrote: > Peter Bailey wrote in post #1091502: >> Hello, >> Can someone help me to write a Ruby one-liner to remove all the files >> except one in a list of files? I have a list of pages of an original >> PDF. Each page has been converted to a PNG file. They're each numbered >> with an underscore and a number at their end. >> >> filename_1.png >> filename_2.png >> filename_3.png >> ... >> >> I need to basically delete all of the files except for the first one, >> the one with the "_1" suffix at the end. Can I do that wiith a >> one-liner? >> >> Thanks, >> Peter > > Test this: > > ruby -e "Dir.foreach('C:/Directory_with_png/') {|file| File.delete(file) > unless (file.==('.')) || (file.==('..') || (file.==('filename_1.png'))" > > Kind regards. > There's certainly many ways to do this, but globing will ignore the dot dirs, and is a bit shorter. File.delete *Dir['*.png'].reject {|f| f =~ /_1\.png$/ }