# One liner for filenames

**URL:** <https://www.ruby-forum.com/t/one-liner-for-filenames/225436>\
**Category:** Ruby\
**Created:** [January 8, 2013, 9:48pm UTC](https://www.ruby-forum.com/t/one-liner-for-filenames/225436 "2013-01-08T21:48:34Z")\
**Posts on this page:** 18\
**Page:** 1

<div class="post-metadata">

**Author:** ![aris](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/258eb7/32.png) [@aris](https://www.ruby-forum.com/u/aris)\
**Post date:** [January 8, 2013, 9:48pm UTC](https://www.ruby-forum.com/t/one-liner-for-filenames/225436/1 "2013-01-08T21:48:34Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![bodikp](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/e79b87/32.png) [@bodikp](https://www.ruby-forum.com/u/bodikp)\
**Post date:** [January 8, 2013, 11:04pm UTC](https://www.ruby-forum.com/t/one-liner-for-filenames/225436/2 "2013-01-08T23:04:37Z")

</div>

Peter B. 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.

---

<div class="post-metadata">

**Author:** ![bodikp](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/e79b87/32.png) [@bodikp](https://www.ruby-forum.com/u/bodikp)\
**Post date:** [January 9, 2013, 12:29am UTC](https://www.ruby-forum.com/t/one-liner-for-filenames/225436/3 "2013-01-09T00:29:59Z")

</div>

> 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

---

<div class="post-metadata">

**Author:** ![bodikp](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/e79b87/32.png) [@bodikp](https://www.ruby-forum.com/u/bodikp)\
**Post date:** [January 9, 2013, 1:08pm UTC](https://www.ruby-forum.com/t/one-liner-for-filenames/225436/4 "2013-01-09T13:08:32Z")

</div>

Kim P. 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.

---

<div class="post-metadata">

**Author:** ![bodikp](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/e79b87/32.png) [@bodikp](https://www.ruby-forum.com/u/bodikp)\
**Post date:** [January 9, 2013, 4:13pm UTC](https://www.ruby-forum.com/t/one-liner-for-filenames/225436/5 "2013-01-09T16:13:19Z")

</div>

Robert K. wrote in post #1091597:

> On Tue, Jan 8, 2013 at 9:48 PM, Peter B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
> 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.

---

<div class="post-metadata">

**Author:** ![bodikp](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/e79b87/32.png) [@bodikp](https://www.ruby-forum.com/u/bodikp)\
**Post date:** [January 9, 2013, 4:15pm UTC](https://www.ruby-forum.com/t/one-liner-for-filenames/225436/6 "2013-01-09T16:15:36Z")

</div>

Kim P. 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

---

<div class="post-metadata">

**Author:** ![bodikp](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/e79b87/32.png) [@bodikp](https://www.ruby-forum.com/u/bodikp)\
**Post date:** [January 9, 2013, 4:10pm UTC](https://www.ruby-forum.com/t/one-liner-for-filenames/225436/7 "2013-01-09T16:10:33Z")

</div>

On Tue, Jan 8, 2013 at 9:48 PM, Peter B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
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

---

<div class="post-metadata">

**Author:** ![bodikp](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/e79b87/32.png) [@bodikp](https://www.ruby-forum.com/u/bodikp)\
**Post date:** [January 9, 2013, 4:23pm UTC](https://www.ruby-forum.com/t/one-liner-for-filenames/225436/8 "2013-01-09T16:23:13Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![bodikp](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/e79b87/32.png) [@bodikp](https://www.ruby-forum.com/u/bodikp)\
**Post date:** [January 9, 2013, 4:56pm UTC](https://www.ruby-forum.com/t/one-liner-for-filenames/225436/9 "2013-01-09T16:56:09Z")

</div>

On Wed, Jan 9, 2013 at 4:48 PM, Jess Gabriel y Galn  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> On Wed, Jan 9, 2013 at 4:09 PM, Robert K.  
> [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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)’

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

Cheers

robert

---

<div class="post-metadata">

**Author:** ![bodikp](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/e79b87/32.png) [@bodikp](https://www.ruby-forum.com/u/bodikp)\
**Post date:** [January 9, 2013, 5:00pm UTC](https://www.ruby-forum.com/t/one-liner-for-filenames/225436/10 "2013-01-09T17:00:52Z")

</div>

“Jesús Gabriel y Galán” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote in post  
#1091607:

> On Wed, Jan 9, 2013 at 4:09 PM, Robert K.  
> [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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?

---

<div class="post-metadata">

**Author:** ![bodikp](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/e79b87/32.png) [@bodikp](https://www.ruby-forum.com/u/bodikp)\
**Post date:** [January 9, 2013, 5:14pm UTC](https://www.ruby-forum.com/t/one-liner-for-filenames/225436/11 "2013-01-09T17:14:51Z")

</div>

On Wed, Jan 9, 2013 at 5:00 PM, Peter B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
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.

---

<div class="post-metadata">

**Author:** ![bodikp](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/e79b87/32.png) [@bodikp](https://www.ruby-forum.com/u/bodikp)\
**Post date:** [January 9, 2013, 5:23pm UTC](https://www.ruby-forum.com/t/one-liner-for-filenames/225436/12 "2013-01-09T17:23:39Z")

</div>

“Jesús Gabriel y Galán” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote in post  
#1091613:

> On Wed, Jan 9, 2013 at 5:00 PM, Peter B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
> 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.

---

<div class="post-metadata">

**Author:** ![bodikp](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/e79b87/32.png) [@bodikp](https://www.ruby-forum.com/u/bodikp)\
**Post date:** [January 9, 2013, 4:49pm UTC](https://www.ruby-forum.com/t/one-liner-for-filenames/225436/13 "2013-01-09T16:49:43Z")

</div>

On Wed, Jan 9, 2013 at 4:09 PM, Robert K.  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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.

---

<div class="post-metadata">

**Author:** ![bodikp](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/e79b87/32.png) [@bodikp](https://www.ruby-forum.com/u/bodikp)\
**Post date:** [January 10, 2013, 8:12am UTC](https://www.ruby-forum.com/t/one-liner-for-filenames/225436/14 "2013-01-10T08:12:15Z")

</div>

On Thu, Jan 10, 2013 at 1:09 AM, Peter B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> > > > > ruby -r pathname -e ‘Pathname.glob(“\*\_{[^1],??}.png”).each(&:delete)’
> > > 
> > > 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 ‘)’

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

Kind regards

robert

---

<div class="post-metadata">

**Author:** ![bodikp](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/e79b87/32.png) [@bodikp](https://www.ruby-forum.com/u/bodikp)\
**Post date:** [January 10, 2013, 10:24am UTC](https://www.ruby-forum.com/t/one-liner-for-filenames/225436/15 "2013-01-10T10:24:22Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![bodikp](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/e79b87/32.png) [@bodikp](https://www.ruby-forum.com/u/bodikp)\
**Post date:** [January 10, 2013, 1:08am UTC](https://www.ruby-forum.com/t/one-liner-for-filenames/225436/16 "2013-01-10T01:08:56Z")

</div>

Peter B. wrote in post #1091614:

> “Jesús Gabriel y Galán” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote in post  
> #1091613:
> 
> > On Wed, Jan 9, 2013 at 5:00 PM, Peter B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
> > 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 ‘)’

---

<div class="post-metadata">

**Author:** ![bodikp](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/e79b87/32.png) [@bodikp](https://www.ruby-forum.com/u/bodikp)\
**Post date:** [January 11, 2013, 11:12am UTC](https://www.ruby-forum.com/t/one-liner-for-filenames/225436/17 "2013-01-11T11:12:21Z")

</div>

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

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

---

<div class="post-metadata">

**Author:** ![bodikp](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/e79b87/32.png) [@bodikp](https://www.ruby-forum.com/u/bodikp)\
**Post date:** [January 10, 2013, 12:58pm UTC](https://www.ruby-forum.com/t/one-liner-for-filenames/225436/18 "2013-01-10T12:58:30Z")

</div>

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?
