# Fugly hex

**URL:** <https://www.ruby-forum.com/t/fugly-hex/108697>\
**Category:** Ruby\
**Created:** [July 6, 2007, 4:45am UTC](https://www.ruby-forum.com/t/fugly-hex/108697 "2007-07-06T04:45:58Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Giles\_B](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/848f3c/32.png) [@Giles\_B](https://www.ruby-forum.com/u/Giles_B)\
**Post date:** [July 6, 2007, 4:45am UTC](https://www.ruby-forum.com/t/fugly-hex/108697/1 "2007-07-06T04:45:58Z")

</div>

Recently I got a hard drive back from a drive recovery place after it  
had died a messy, violent, public death, like a Sopranos guest star.

I had to restore 65,536 directories.

Each directory contained some unknown (possibly zero) number of files.

The files were stored in directories with single-character names  
running 0-f (hexadecimal).

My xargs fu was then too weak to handle this with a simple cp -r in  
Unix. The argument list was too long. Additionally, the 65,536  
directories did not yet exist in the new location.

The solution was pretty quick to implement:

hex = (0…9).to\_a + %w{a b c d e f}

hex.each do |first|  
system “mkdir /new/#{first}”  
hex.each do |second|  
system “mkdir /new/#{first}/#{second}”  
hex.each do |third|  
system “mkdir /new/#{first}/#{second}/#{third}”  
hex.each do |fourth|  
system “mkdir /new/#{first}/#{second}/#{third}/#{fourth}”  
system “cp /original/#{first}/#{second}/#{third}/#{fourth}/\*  
/new/#{first}/#{second}/#{third}/#{fourth}/”  
end  
end  
end  
end

The hardest part was being satisfied with the implementation, and  
resisting the urge to improve it. There’s a great deal of repetition  
there.

How can I make it prettier?

–  
Giles B.

Blog: [http://gilesbowkett.blogspot.com](http://gilesbowkett.blogspot.com)  
Portfolio: [http://www.gilesgoatboy.org](http://www.gilesgoatboy.org)

---

<div class="post-metadata">

**Author:** ![Giles\_B](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/848f3c/32.png) [@Giles\_B](https://www.ruby-forum.com/u/Giles_B)\
**Post date:** [July 6, 2007, 4:51am UTC](https://www.ruby-forum.com/t/fugly-hex/108697/2 "2007-07-06T04:51:58Z")

</div>

On 7/5/07, Giles B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> My xargs fu was then too weak to handle this with a simple cp -r in  
> system “mkdir /new/#{first}/#{second}”
> 
> The hardest part was being satisfied with the implementation, and  
> resisting the urge to improve it. There’s a great deal of repetition  
> there.

use fileutils instead of system, and move first\_second\_third\_forth  
into a var. Then use mkdir\_p instead of mkdir to avoid the first  
three calls.

that’s my ‘quick glance’ suggestion

---

<div class="post-metadata">

**Author:** ![Giles\_B](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/848f3c/32.png) [@Giles\_B](https://www.ruby-forum.com/u/Giles_B)\
**Post date:** [July 6, 2007, 5:06am UTC](https://www.ruby-forum.com/t/fugly-hex/108697/3 "2007-07-06T05:06:43Z")

</div>

Sure  
require ‘fileutils’ # as Gregory B. already stated  
65536.times { |x|  
FileUtils.mkdir(“new/%x/%x/%x/%x” % [x \>\> 12, x \>\> 8, x \>\> 4, x].map {  
|x| x & 0xf })  
}

Have fun  
Stefan

Giles B. wrote:

> Recently I got a hard drive back from a drive recovery place after it  
> had died a messy, violent, public death, like a Sopranos guest star.
> 
> I had to restore 65,536 directories.
> 
> Each directory contained some unknown (possibly zero) number of files.
> 
> The files were stored in directories with single-character names  
> running 0-f (hexadecimal).
> 
> My xargs fu was then too weak to handle this with a simple cp -r in  
> Unix. The argument list was too long. Additionally, the 65,536  
> directories did not yet exist in the new location.
> 
> The solution was pretty quick to implement:
> 
> hex = (0…9).to\_a + %w{a b c d e f}
> 
> hex.each do |first|  
> system “mkdir /new/#{first}”  
> hex.each do |second|  
> system “mkdir /new/#{first}/#{second}”  
> hex.each do |third|  
> system “mkdir /new/#{first}/#{second}/#{third}”  
> hex.each do |fourth|  
> system “mkdir /new/#{first}/#{second}/#{third}/#{fourth}”  
> system “cp /original/#{first}/#{second}/#{third}/#{fourth}/\*  
> /new/#{first}/#{second}/#{third}/#{fourth}/”  
> end  
> end  
> end  
> end
> 
> The hardest part was being satisfied with the implementation, and  
> resisting the urge to improve it. There’s a great deal of repetition  
> there.
> 
> How can I make it prettier?

---

<div class="post-metadata">

**Author:** ![Giles\_B](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/848f3c/32.png) [@Giles\_B](https://www.ruby-forum.com/u/Giles_B)\
**Post date:** [July 6, 2007, 7:40pm UTC](https://www.ruby-forum.com/t/fugly-hex/108697/4 "2007-07-06T19:40:22Z")

</div>

On 7/5/07, Stefan R. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Sure  
> require ‘fileutils’ # as Gregory B. already stated  
> 65536.times { |x|  
> FileUtils.mkdir(“new/%x/%x/%x/%x” % [x \>\> 12, x \>\> 8, x \>\> 4, x].map {  
> |x| x & 0xf })  
> }

Excellent. I tested it in irb as a one-liner:

65\_536.times {|x| puts(“new/%x/%x/%x/%x” % [x \>\> 12, x \>\> 8, x \>\> 4,  
x].map {|x| x & 0xf })}

But how does that work? Is there a way to make it more readable for  
the programmer with no understanding of bit-shifting? (That would be  
me, specifically.) Is there a way to make bit-shifting simple and  
obvious?

Also, there’s a showstopping flaw. It starts with trying to do:

FileUtils.mkdir “0/0/0/0”

But this throws an error in either Unix or irb, because you have to  
mkdir 0 before you put the next 0 in it, and so on four levels down.  
That’s why I went with the nested each statements.

In words, what you need to do is, every time you get past the f at the  
top of hexadecimal’s 0-f, add a new level, and when you get to 4  
levels, start copying files.

Is there a way to do it recursively, with an awareness of the level of  
recursion?

–  
Giles B.

Blog: [http://gilesbowkett.blogspot.com](http://gilesbowkett.blogspot.com)  
Portfolio: [http://www.gilesgoatboy.org](http://www.gilesgoatboy.org)

---

<div class="post-metadata">

**Author:** ![Giles\_B](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/848f3c/32.png) [@Giles\_B](https://www.ruby-forum.com/u/Giles_B)\
**Post date:** [September 25, 2007, 11:10pm UTC](https://www.ruby-forum.com/t/fugly-hex/108697/5 "2007-09-25T23:10:53Z")

</div>

On 7/6/07, Giles B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Also, there’s a showstopping flaw. It starts with trying to do:
> 
> FileUtils.mkdir “0/0/0/0”
> 
> But this throws an error in either Unix or irb, because you have to  
> mkdir 0 before you put the next 0 in it, and so on four levels down.  
> That’s why I went with the nested each statements.

FileUtils.mkdir\_p “0/0/0/0” # should do the trick
