Monthly Archives: August 2019

Delete old files from huge directory with perl

If you need to delete old files from a directory but ls/find/rm/etc hang because of the number of files (especially if they’re on a slow NFS share), then you can use perl!

chdir "DIRECTORY" or die;
opendir D, ".";
$count = 0;
while ($n = readdir D) {
    $days = -M $n;
    if ($days > 3){
        #print "deleting $n $days\n";
        $count++;
        unlink $n or warn "Can't unlink $file: $!";
    }
}
print "deleted $count files\n";

Replace DIRECTORY with your directory name, and 3 with the max age of the files to keep (here it will keep files 3 days old or less).

If you’re running this on AWS EFS then temporarily switch the filesystem throughput mode from burstable to provisioned, otherwise it will start quick but get very very slow once you’re out of burstable credits.