use strict; use warnings; use File::Spec; use File::Path qw/mkpath/; my $file = shift; my $first = substr( $file, 0, 1 ); my $second = substr( $file, 0, 2 ); my $path = File::Spec->catdir( $first, $second ); mkpath( $path ); rename $file, File::Spec->catfile( $path, $file ) or die "Can't file $file\: $!";