How do I merge two lists with different sort methods?
April 22, 2010 2:26 PM   Subscribe

How can I merge two directory trees, one arranged "Last, First", the other arranged "First Last"?

On my mac, I've ended up with two directory trees, one that contains sub-directories of authors that are listed in "Last, First" format, the other in "First Last". Ideally, I'd like everything to end up in the "Last, First" set of directories.

Additional level of difficulty, there are too many authors to have them all in the same directory, under either sorting, so both trees have an intermediate stage where everything is sorted by first letter, ie

dir1/A/Aanderson, James/
dir2/J/James Anderson/

dirx/A/Aanderson, James/
dirx/A/Aanderson, James/
posted by nomisxid to Computers & Internet (5 answers total)
Would help if you told us what operating system you were on. Makes a big difference in terms of what tools are available to accomplish this.
posted by spitefulcrow at 3:07 PM on April 22, 2010

Okay, on second look, you've tagged it Mac.

How's your bash-fu? This is pretty easy if you know your way around a command-line shell. You can use sed and/or awk to chop up the input path names and spit them into a mv command. Post back if you've no idea what the hell I'm talking about, and I can probably dig up some primers to get you pointed in the right direction.
posted by spitefulcrow at 3:10 PM on April 22, 2010

Try A Better finder Rename if you want a GUI. There's a free trial. It appears to allow RegExes in which case you'd want to look for something like "(.*), (.*)" and replace it with something like "\2 \1."
posted by kindall at 3:38 PM on April 22, 2010

Dear nomisxid,

Run the following Perl script in the directory that contains dir1, dir2, etc.
It will only print the commands it intends to run, unless you set $I_FEEL_SAFE_RUNNING_THIS to 1.

Disclaimer: this worked on a really tiny test case I set up, but I'd run this on a copy of your directory structure first just to be safe. I am not responsible for anything that might happen to your computer as a result of running this.
@files = split(/\n/, `find dir*/ -type f`);


for $file (@files) {
    $file =~ s/\/\//\//g; # Get rid of double slashes
    $file =~ s/\s/\\ /g; # Escape spaces

    @parts = split(/\//, $file);

    $parts[0] = "dirx"; # The party has moved to dirx
    $parts[2] =~ s/(\w+)\\\s(\w+)/$2,\\ $1/; # Switch to Last, First
    $parts[1] = uc(substr($parts[2], 0, 1)); # Figure out the new index letter

    $new_dir = join('/', @parts[0..($#parts-1)]);
    $new_file = join('/', @parts);

        system("mkdir -p $new_dir\n");
        system("mv $file $new_file\n");
    } else {
        print "mkdir -p $new_dir\n";
        print "mv $file $new_file\n";


P.S. I know Perl is ugly, but it's hard to beat for quick-n-dirty regexes...
posted by caaaaaam at 4:01 PM on April 22, 2010

That was really nice of you, caaaaaam, to take the time to solve nomisxid's problem. Thanks on his behalf.
posted by gum at 10:03 PM on April 23, 2010

« Older Music that makes you feel like you could walk...   |   Hard drive to use with Fostex D2424LV MKII? Newer »
This thread is closed to new comments.