#!/usr/bin/env perl # subdivide.pl - given a few configurations, divide a directory into many smaller ones # see: https://stackoverflow.com/questions/36511098/split-large-directory-into-subdirectories # configure my $BASE = "json"; my $ROOT = './jsons'; my $LIMIT = 10000; my $i = 0; my $directory = ''; # require use strict; use File::Copy; # open the directory and process each file opendir( my $handle, $BASE ) or die( "Can't open $BASE: $!\n"); while ( my $file = readdir( $handle ) ) { # sanity check next if $file !~ /json$/; warn( " file: $file\n" ); # check for new directory needed if ( $i % $LIMIT == 0 ) { # create a new directory $directory = sprintf( "%s/json_%03d", $ROOT, ( int( $i/$LIMIT ) + 1 ) ); warn( " directory: $directory\n" ); mkdir( $directory ) or warn( "Can't make directory $directory: $!\n" ); } # copy the given file to the new directory copy( "$BASE/$file", "$directory/$file" ) or do { warn("Can't move $file into $directory: $!\n"); next; }; # increment $i++; }