# One-liner for lowercasing files

**URL:** <https://www.ruby-forum.com/t/one-liner-for-lowercasing-files/180198>\
**Category:** Ruby\
**Created:** [December 14, 2009, 3:23pm UTC](https://www.ruby-forum.com/t/one-liner-for-lowercasing-files/180198 "2009-12-14T15:23:32Z")\
**Posts on this page:** 8\
**Page:** 1

<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:** [December 14, 2009, 3:23pm UTC](https://www.ruby-forum.com/t/one-liner-for-lowercasing-files/180198/1 "2009-12-14T15:23:32Z")

</div>

Hi,  
Can someone please suggest a simple Ruby one-liner to lowercase a  
filename (in a directory)? I’ve perused, with Google, a bunch of good  
Ruby one-liner sites, but, I can’t find any lowercasing examples.

Thanks a lot,  
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:** [December 14, 2009, 3:55pm UTC](https://www.ruby-forum.com/t/one-liner-for-lowercasing-files/180198/2 "2009-12-14T15:55:10Z")

</div>

Peter B. wrote:

> Can someone please suggest a simple Ruby one-liner to lowercase a  
> filename (in a directory)?

Lowercase the _contents_ of a file, or its _name_ ?

For the contents, this example lifted almost verbatim from “man ruby”.

ruby -p -i.bak -e ‘$\_.downcase!’ /tmp/junk

To lowercase the names of all files:

Dir["\*"].each { |fn| File.rename(fn, fn.downcase) }

Beware that even ruby 1.9 won’t lowercase anything other than plain A-Z.

---

<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:** [December 14, 2009, 5:47pm UTC](https://www.ruby-forum.com/t/one-liner-for-lowercasing-files/180198/3 "2009-12-14T17:47:19Z")

</div>

Thanks, Brian. Yes, it’s just filename I want, not content. But, this is  
what I get when I try your suggestion:

> ruby Dir.glob("\*").each { |fn| File.rename(fn, fn.downcase) }  
> ‘file’ is not recognized as an internal or external command,  
> operable program or batch file.

Brian C. wrote:

> Peter B. wrote:
> 
> > Can someone please suggest a simple Ruby one-liner to lowercase a  
> > filename (in a directory)?
> 
> Lowercase the _contents_ of a file, or its _name_ ?
> 
> For the contents, this example lifted almost verbatim from “man ruby”.
> 
> ruby -p -i.bak -e ‘$\_.downcase!’ /tmp/junk
> 
> To lowercase the names of all files:
> 
> Dir["\*"].each { |fn| File.rename(fn, fn.downcase) }
> 
> Beware that even ruby 1.9 won’t lowercase anything other than plain A-Z.

---

<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:** [December 14, 2009, 6:08pm UTC](https://www.ruby-forum.com/t/one-liner-for-lowercasing-files/180198/4 "2009-12-14T18:08:43Z")

</div>

On Dec 14, 2009, at 1:17 PM, Peter B. wrote:

> Thanks, Brian. Yes, it’s just filename I want, not content. But,  
> this is  
> what I get when I try your suggestion:
> 
> > ruby Dir.glob("\*").each { |fn| File.rename(fn, fn.downcase) }  
> > ‘file’ is not recognized as an internal or external command,  
> > operable program or batch file.

Try

ruby -e ‘Dir.glob("\*").each { |fn| File.rename(fn, fn.downcase) }’

Otherwise, Dir.glob… will be seen as a shell command, not as ruby  
code.

Regards,  
Florian

---

<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:** [December 14, 2009, 10:00pm UTC](https://www.ruby-forum.com/t/one-liner-for-lowercasing-files/180198/5 "2009-12-14T22:00:31Z")

</div>

On Dec 14, 12:04 pm, Peter B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Still no good. Here’s what I get:
> 
> > ruby -e ‘Dir.glob(“_._”).each { |fn| File.rename(fn, fn.downcase) }’
> 
> ‘fn’ is not recognized as an internal or external command,  
> operable program or batch file.
> 
> E:\Asura-Non\BWDhelp\temp\>

Do not top-post.

Since you are using the windoze shell, you must learn some things  
about the windoze shell. Does that make sense?

In this case, the Ruby code must be enclosed in double quotes.  
Try this.

ruby -e “Dir[‘\*’].each{|fn| p fn.downcase }”

---

<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:** [December 14, 2009, 10:09pm UTC](https://www.ruby-forum.com/t/one-liner-for-lowercasing-files/180198/6 "2009-12-14T22:09:56Z")

</div>

w\_a\_x\_man wrote:

> On Dec 14, 12:04ï¿½pm, Peter B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > Still no good. Here’s what I get:
> > 
> > > ruby -e ‘Dir.glob(“_._”).each { |fn| File.rename(fn, fn.downcase) }’
> > 
> > ‘fn’ is not recognized as an internal or external command,  
> > operable program or batch file.
> > 
> > E:\Asura-Non\BWDhelp\temp\>
> 
> Do not top-post.
> 
> Since you are using the windoze shell, you must learn some things  
> about the windoze shell. Does that make sense?
> 
> In this case, the Ruby code must be enclosed in double quotes.  
> Try this.
> 
> ruby -e “Dir[‘\*’].each{|fn| p fn.downcase }”

Here’s what worked. Yours worked, waxman, but I needed a real filename  
conversion, not just a print of it.  
Thank you all for your help! This is great, simple stuff.

> ruby -e “Dir.glob(‘\*’).each{|fn| File.rename(fn, fn.downcase) }”

---

<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:** [December 14, 2009, 7:02pm UTC](https://www.ruby-forum.com/t/one-liner-for-lowercasing-files/180198/7 "2009-12-14T19:02:13Z")

</div>

Still no good. Here’s what I get:

> ruby -e ‘Dir.glob("_._").each { |fn| File.rename(fn, fn.downcase) }’  
> ‘fn’ is not recognized as an internal or external command,  
> operable program or batch file.

E:\Asura-Non\BWDhelp\temp\>

Florian G. wrote:

> On Dec 14, 2009, at 1:17 PM, Peter B. wrote:
> 
> > Thanks, Brian. Yes, it’s just filename I want, not content. But,  
> > this is  
> > what I get when I try your suggestion:
> > 
> > > ruby Dir.glob("\*").each { |fn| File.rename(fn, fn.downcase) }  
> > > ‘file’ is not recognized as an internal or external command,  
> > > operable program or batch file.
> 
> Try
> 
> ruby -e ‘Dir.glob("\*").each { |fn| File.rename(fn, fn.downcase) }’
> 
> Otherwise, Dir.glob… will be seen as a shell command, not as ruby  
> code.
> 
> Regards,  
> Florian

---

<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:** [December 15, 2009, 11:13am UTC](https://www.ruby-forum.com/t/one-liner-for-lowercasing-files/180198/8 "2009-12-15T11:13:35Z")

</div>

2009/12/14 Peter B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> > 
> 
> Here’s what worked. Yours worked, waxman, but I needed a real filename  
> conversion, not just a print of it.  
> Thank you all for your help! This is great, simple stuff.
> 
> > ruby -e “Dir.glob(‘\*’).each{|fn| File.rename(fn, fn.downcase) }”

Note that if you use a directory name in Dir[‘\*’] you need to make  
sure the dirname remains unchanged. Then you might rather do

ruby -e ‘Dir[“path/\*”].each {|f| d, p = File.split(f); File.rename(f,  
File.join(d, p.downcase))}’

Kind regards

robert
