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?
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?
Excellent. Thanks. Hans is right, though. Dir.glob is unsorted. Is that
a problem here?
No, because here we are using the glob facility to do all the
filtering. In the other solution, there was the array returned from
the glob that was being filtered, and so the order was important for
the solution that removed the first entry in the array.
Excellent. Thanks. Hans is right, though. Dir.glob is unsorted. Is that
a problem here?
No, because here we are using the glob facility to do all the
filtering. In the other solution, there was the array returned from
the glob that was being filtered, and so the order was important for
the solution that removed the first entry in the array.
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$/ }
Excellent. Thanks. Hans is right, though. Dir.glob is unsorted. Is that
a problem here?
No, because here we are using the glob facility to do all the
filtering. In the other solution, there was the array returned from
the glob that was being filtered, and so the order was important for
the solution that removed the first entry in the array.
Jesus.
Beautiful. Thanks a lot.
This is what I’m getting. I’m sure i’m missing something simple.
dirs, and is a bit shorter.
File.delete Dir[’.png’].reject {|f| f =~ /_1.png$/ }
Thanks, Brian. But, even preceding this with “ruby -e” gives me nothing.
Should I make this part of a script instead of it being a one-liner?
That’s odd… Works for me under 1.8.7, 1.9.2 and 1.9.3.
ruby -e “File.delete Dir[’.png’].reject {|f| f=~ /_1.png$/ }”
There will be no output. It will just delete the files.
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$/ }
Thanks, Brian. But, even preceding this with “ruby -e” gives me nothing.
Should I make this part of a script instead of it being a one-liner?
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.