-
-
Notifications
You must be signed in to change notification settings - Fork 9
/
rmtpy
executable file
·54 lines (45 loc) · 1.08 KB
/
rmtpy
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#!/usr/bin/perl
# vim:ft=perl:
# abstract: given a root directory, remove empty directories or directories one level deep with only empty files in them
#
# rmpty /mnt/Music_1/.new/
use strict;
use warnings FATAL => 'all';
use File::Find::Rule;
use Cwd;
use File::Path qw(remove_tree);
use File::LsColor qw(ls_color);
my $dir = shift // './';
my @subdirs = grep{! /\A[.][.]?\z/ } File::Find::Rule->directory->in( $dir );
printf("Found \e[33m%d\e[m subdirs in %s\n",
scalar(@subdirs), ls_color($dir),
);
my %empty = ();
for my $d(@subdirs) {
if(!chdir($d)) {
warn("skipping $d\n");
next;
}
my $sum = 0;
for my $file(File::Find::Rule->file->in($d)) {
$sum += -s $file;
}
$empty{$d} = $sum if $sum < 2048;
}
for my $path(sort { $empty{$b} <=> $empty{$a} } keys(%empty)) {
printf("Remove %s [y/N] ", ls_color($path));
chomp(my $choice = <>);
if( lc($choice) ne 'y' ) {
next;
}
else {
remove_tree($path, {
result => \my $progress,
},
);
for(@{ $progress }) {
printf("\e[1m>\e[m %s\n", ls_color( $_ ));
}
print "-\n";
}
}