Gregory Brown
7/6/2007 2:51:00 AM
On 7/5/07, Giles Bowkett <gilesb@gmail.com> 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.
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