#!/usr/bin/perl use strict; use warnings; chomp(my @list = `du -kh /mnt/edi/si51/documents`); my $dltd = 0; foreach my $line (@list) { my ($size,$path) = split /\t/, $line; $size =~ s/\D//g; if ($size == 0) { rmdir $path && $dltd++ } } printf "%d directories deleted.\n",$dltd;
UPDATE: There are several things that were in an earlier version of this script that didn't make the second cut, but only because I got lazy. My original got deleted somehow, and I had foolishly not kept a copy, so I wrote the above quickly.
The directory structure is documents/4digityear/abbreviatedcardinalmonth/2digitday/hour/minute. At first I restricted deletions to directories above some number of days old, but rmdir updates the directory time information, meaning a directory that was now empty because all its empty constituent directories were gone looked like it was brand new. This made it useless to run consecutively. I came up with a calculation that used the directory tree to come up with the age, and that worked. I just didn't bother with it when I rewrote the script this time. Some of the alternate solutions don't have that limitation.
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: Clean Up Empty Directories
by choroba (Cardinal) on Feb 16, 2018 at 23:35 UTC | |
by Anonymous Monk on Feb 18, 2018 at 16:17 UTC | |
by GotToBTru (Prior) on Feb 19, 2018 at 19:25 UTC | |
Re: Clean Up Empty Directories
by Tux (Canon) on Feb 17, 2018 at 10:18 UTC | |
by GotToBTru (Prior) on Feb 19, 2018 at 19:21 UTC | |
Re:Clean Up Empty Directories
by tybalt89 (Monsignor) on Feb 16, 2018 at 22:15 UTC | |
Re: Clean Up Empty Directories
by morgon (Priest) on Feb 17, 2018 at 02:24 UTC | |
by GotToBTru (Prior) on Feb 19, 2018 at 19:18 UTC |