#!/usr/local/bin/perl use strict; use warnings; use MIME::Base64 qw(decode_base64); use File::Path; my ($uploads) = @ARGV; my @uploads = map { glob($_ . '/*') } map {glob($_ . '/*')} glob($uploads . '/*'); for my $p (@uploads) { $p =~ m{([^/]*$)}; my $id = $1; # find final portion of path next unless length($id) == 22; # skip if not using webgui guid format $id =~ tr{_-}{+/}; # convert to base64 string my $bin_id = decode_base64("$id=="); # convert to binary my $hex_id = sprintf('%*v02x', '', $bin_id); # convert to hex $hex_id =~ /^(.{2})(.{2})/; # pick out path sections my $path = join('/', $uploads, $1, $2, $hex_id); # convert to final path mkpath($path); # create path opendir(my $dh, $p); while (my $file = readdir($dh)) { next if $file =~ /^\.+$/; system("git mv $p/$file $path/$file"); # move files } closedir $dh; } system("find $uploads -empty -delete");