One liner for filenames

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

···

--
Posted via http://www.ruby-forum.com/.

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.

···

--
Posted via http://www.ruby-forum.com/\.

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

Hi,

This should work…

ruby -e "Dir.glob('*_*.png')[1..-1].each { |fn| File.delete fn}"

Best regards
Kim

ruby -r pathname -e 'Pathname.glob("*[^1].png").each(&:delete)'

Kind regards

robert

···

On Tue, Jan 8, 2013 at 9:48 PM, Peter Bailey <lists@ruby-forum.com> wrote:

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?

--
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/

about Array#[]:

[1,2,3,4,5][1..-1] #=> [2,3,4,5]

about Dir.glob():

you need to be careful, because the array is unsorted

···

--
Posted via http://www.ruby-forum.com/.

Kim Pedersen wrote in post #1091524:

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

Hi,

This should work

ruby -e "Dir.glob('*_*.png')[1..-1].each { |fn| File.delete fn}"

Best regards
Kim

Thank you very much, Kim. It's a big help.
Cheers.

···

--
Posted via http://www.ruby-forum.com/\.

Robert Klemme wrote in post #1091597:

···

On Tue, Jan 8, 2013 at 9:48 PM, Peter Bailey <lists@ruby-forum.com> > wrote:

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?

ruby -r pathname -e 'Pathname.glob("*[^1].png").each(&:delete)'

Kind regards

robert

Thanks, Robert.

--
Posted via http://www.ruby-forum.com/\.

Kim Pedersen wrote in post #1091524:

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

Hi,

This should work

ruby -e "Dir.glob('*_*.png')[1..-1].each { |fn| File.delete fn}"

Best regards
Kim

Kim,
I'd like to ask you about [1..-1]. I've never seen that before. Is that
what's selecting all the files except the first one?
-Peter

···

--
Posted via http://www.ruby-forum.com/\.

Careful, as this skips also filename_11.png, filename_21.png, etc.
Building on this:

ruby -r pathname -e 'Pathname.glob("*_{[^1],??}.png").each(&:delete)'

Jesus.

···

On Wed, Jan 9, 2013 at 4:09 PM, Robert Klemme <shortcutter@googlemail.com> wrote:

On Tue, Jan 8, 2013 at 9:48 PM, Peter Bailey <lists@ruby-forum.com> wrote:

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?

ruby -r pathname -e 'Pathname.glob("*[^1].png").each(&:delete)'

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$/ }

···

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.

+1 I wasn't aware that there could be larger numbers. Thank you for
the correction!

Cheers

robert

···

On Wed, Jan 9, 2013 at 4:48 PM, Jesús Gabriel y Galán <jgabrielygalan@gmail.com> wrote:

On Wed, Jan 9, 2013 at 4:09 PM, Robert Klemme > <shortcutter@googlemail.com> wrote:

ruby -r pathname -e 'Pathname.glob("*[^1].png").each(&:delete)'

Careful, as this skips also filename_11.png, filename_21.png, etc.
Building on this:

ruby -r pathname -e 'Pathname.glob("*_{[^1],??}.png").each(&:delete)'

--
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/

"Jesús Gabriel y Galán" <jgabrielygalan@gmail.com> wrote in post
#1091607:

···

On Wed, Jan 9, 2013 at 4:09 PM, Robert Klemme > <shortcutter@googlemail.com> wrote:

...

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?

ruby -r pathname -e 'Pathname.glob("*[^1].png").each(&:delete)'

Careful, as this skips also filename_11.png, filename_21.png, etc.
Building on this:

ruby -r pathname -e 'Pathname.glob("*_{[^1],??}.png").each(&:delete)'

Jesus.

Excellent. Thanks. Hans is right, though. Dir.glob is unsorted. Is that
a problem here?

--
Posted via http://www.ruby-forum.com/\.

Brian D. B. wrote in post #1091712:

···

On 01/08/2013 05:04 PM, Damián M. González wrote:

...

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$/ }

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?

--
Posted via http://www.ruby-forum.com/\.

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.

···

On Wed, Jan 9, 2013 at 5:00 PM, Peter Bailey <lists@ruby-forum.com> wrote:

"Jesús Gabriel y Galán" <jgabrielygalan@gmail.com> wrote in post
#1091607:

On Wed, Jan 9, 2013 at 4:09 PM, Robert Klemme >> <shortcutter@googlemail.com> wrote:

...

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?

ruby -r pathname -e 'Pathname.glob("*[^1].png").each(&:delete)'

Careful, as this skips also filename_11.png, filename_21.png, etc.
Building on this:

ruby -r pathname -e 'Pathname.glob("*_{[^1],??}.png").each(&:delete)'

Jesus.

Excellent. Thanks. Hans is right, though. Dir.glob is unsorted. Is that
a problem here?

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.

···

On 01/10/2013 06:58 AM, Peter Bailey wrote:

Brian D. B. wrote in post #1091712:

On 01/08/2013 05:04 PM, Damián M. González wrote:

...

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$/ }

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?

"Jesús Gabriel y Galán" <jgabrielygalan@gmail.com> wrote in post
#1091613:

···

On Wed, Jan 9, 2013 at 5:00 PM, Peter Bailey <lists@ruby-forum.com> > wrote:

ruby -r pathname -e 'Pathname.glob("*[^1].png").each(&:delete)'

Careful, as this skips also filename_11.png, filename_21.png, etc.
Building on this:

ruby -r pathname -e 'Pathname.glob("*_{[^1],??}.png").each(&:delete)'

Jesus.

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.

--
Posted via http://www.ruby-forum.com/\.

Peter Bailey wrote in post #1091614:

"Jesús Gabriel y Galán" <jgabrielygalan@gmail.com> wrote in post
#1091613:

ruby -r pathname -e 'Pathname.glob("*[^1].png").each(&:delete)'

Careful, as this skips also filename_11.png, filename_21.png, etc.
Building on this:

ruby -r pathname -e 'Pathname.glob("*_{[^1],??}.png").each(&:delete)'

Jesus.

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.

ruby -r pathname -e 'Pathname.glob("*_{[^1],??}.png").each(&:delete)'
-e:1: syntax error, unexpected $end, expecting ')'

···

On Wed, Jan 9, 2013 at 5:00 PM, Peter Bailey <lists@ruby-forum.com> >> wrote:

--
Posted via http://www.ruby-forum.com/\.

Any chance you are using Ruby 1.8*? If so I suggest to upgrade to 1.9*.

Kind regards

robert

···

On Thu, Jan 10, 2013 at 1:09 AM, Peter Bailey <lists@ruby-forum.com> wrote:

Peter Bailey wrote in post #1091614:

"Jesús Gabriel y Galán" <jgabrielygalan@gmail.com> wrote in post
#1091613:

On Wed, Jan 9, 2013 at 5:00 PM, Peter Bailey <lists@ruby-forum.com> >>> wrote:

ruby -r pathname -e 'Pathname.glob("*[^1].png").each(&:delete)'

Careful, as this skips also filename_11.png, filename_21.png, etc.
Building on this:

ruby -r pathname -e 'Pathname.glob("*_{[^1],??}.png").each(&:delete)'

Jesus.

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.

ruby -r pathname -e 'Pathname.glob("*_{[^1],??}.png").each(&:delete)'
-e:1: syntax error, unexpected $end, expecting ')'

--
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/