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
Portfolio: http://www.gilesgoatboy.org