-
Notifications
You must be signed in to change notification settings - Fork 4
/
diskuse
executable file
·452 lines (400 loc) · 12.4 KB
/
diskuse
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
#!/usr/bin/perl
# find out what's filling up a partition
# Mikel Ward <[email protected]>
require 5.6.0;
use Getopt::Long qw(:config no_ignore_case bundling);
my @fileinfo; # [path, size]
my @dirinfo;
our %dirsize; # path => total size of files at that level
our %dirfileinfo;
our %seen; # $seen{$dev . $inode} = 1 if seen, else undef
# tracks which files were already seen to avoid counting twice or going into a loop
our $pathwidth; # maximum width to use for printing a file name
our $apparentsize = 0;
our $debug = 0;
our $groupbydir = 0;
our $minsize = 0;
our $quiet = 0;
# it's a string, so remember that \\ becomes \
#our $pathskip = "^/proc\\b|^/dev\\b|^/sys\\b|^/lib/udev\\b|^/net\\b|\\bfd/\\d+\\b|\\bdosdevices\\b|\\b\\.gvfs\\b";
our $pathskip;
our $maxage;
our $dirsonly = 0;
our $singledevice = 1;
our $sizeunits = "MB";
our $subdirsize = 0;
our $showdirtotals = 1;
# TODO try to get the terminal width (can't use $ENV{'TERM'} because $TERM is not exported)
our $verbose = 0;
our $width;
use constant BLKSIZE => 512;
sub usage
{
print STDERR "
Usage: diskuse <directory or mount point>
Options:\
-a <time> Only look at files and directories modified in the last <time> seconds
-A Look at all files and directories, irrespective of age (like du)
-f Don't include directory totals in the list (report on files only)
-h Print this help message
-s <size> Don't print anything smaller than <size> bytes (default $minsize)
-t Include directory totals in the list (default)
-u <size unit> Print sizes in <size unit>, one of B/K/M/G/T
Advanced options:
-b Print sizes based on file size (like ls), not blocks used (like du)
-d Print debugging information
-g Group by directory (best if directories might contain lots of small files)
-p <pattern> Skip directories matching <pattern>
-P Disable pathskip
-q Don't print error messages about non-readable directories (not recommended)
-S Include size of sub-directories in directory size (like du)
-T Show only directory totals (like du)
-v Print more information about what is being done
-w <characters> Set the screen width to <characters> characters (instead of automatic)
-x Skip directories on different devices (default)
-X Don't skip directories on different devices
Notes:
Times can be specified with a s (seconds), m (minutes), h (hours), d (days), or w (weeks) suffix
Sizes can be specified with a B (byte), K (kilobyte), M (megabyte), G (gigabyte), or T (terabyte) suffix
Example:
diskuse -s 100M ~ (list the biggest files under your home directory)
diskuse -a 1h -s 10M /var (find out what's changed in the last hour on /var)
";
}
GetOptions(
"apparent-size|bytes|filesize|b" => \$apparentsize,
"debug|d" => \$debug,
"help|h" => sub { usage; exit 0 },
"age|maxage|a=s" => \$maxage,
"nomaxage|all|A" => sub { $maxage = undef },
"filesonly|f" => sub { $showdirtotals = 0 },
"group|g" => \$groupbydir,
"pathskip|skip|skippattern|p=s" => \$pathskip,
"nopathskip|noskip|P" => sub { $pathskip = undef },
"quiet|q" => \$quiet,
"singledevice|x!" => \$singledevice,
"multidevice|X" => sub { $singledevice = 0; },
"size|minsize|s=s" => \$minsize,
"subdirsize|S" => \$subdirsize,
"units|u=s" => \$sizeunits,
"dirtotals|totals|t" => \$showdirtotals,
"dirsonly|onlydirs|dirtotalsonly|totalsonly|T" => \$dirsonly,
"verbose|v" => \$verbose,
"width|w=i" => \$width)
or usage, exit 2;
if (@ARGV < 1) {
print STDERR "Directory name or mount point is required\n";
usage;
exit(2);
}
my $root = $ARGV[0];
print STDERR "Reporting on $root\n";
sub process_dir
{
my ($dir, $fileinforef) = @_;
my $dirhandle;
my $now = time;
# avoid recursing into $root via /dev/fd/n
# now handled via pathskip
#next if $path eq "/dev/fd";
#next if $path eq "/dev/.static/dev/fd";
#next if ($path =~ m{/fd/\d});
if (defined($pathskip) && $pathskip && ($dir =~ m{$pathskip})) {
print STDERR "Skipping $dir (matches the -p option pattern)\n" if $debug;
return;
}
# if ($showspecial) {
# next if $path eq "/proc";
# next if $path eq "/dev";
# next if $path eq "/sys";
# }
my ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,$atime,$mtime,$ctime,$blksize,$blocks) = stat($dir);
my $dirdev = $dev;
if (!$apparentsize) {
# have to multiply by BLKSIZE, which is not necessarily $blksize
# (BLKSIZE is 512 on Linux and BSD, irrespective of $blksize)
$size = $blocks * BLKSIZE;
}
print STDERR "Examining $dir\n" if $verbose;
if (! -r _) {
print STDERR "Can't read $dir\n" if ! $quiet;
return 0;
}
$dirsize{$dir} += $size;
if (opendir($dirhandle, "$dir")) {
while (my $dirent = readdir($dirhandle)) {
next if $dirent eq ".";
next if $dirent eq "..";
my $path = ($dir eq "/" ? "/" : $dir . "/") . $dirent;
my ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,$atime,$mtime,$ctime,$blksize,$blocks) = lstat($path);
if (-l _) {
print STDERR "Skipping $path (symbolic link)\n" if $debug;
next;
}
if (!$apparentsize) {
# have to multiply by BLKSIZE, which is not necessarily $blksize
# (BLKSIZE is 512 on Linux and BSD, irrespective of $blksize)
$size = $blocks * BLKSIZE;
}
if (!$dev || !$ino) {
print STDERR "Skipping $path (not found)\n" if ! $quiet;
next;
}
if (my $alias = $seen{$dev . $ino}) {
print STDERR "Skipping $path (same as $alias - dev=$dev ino=$ino)\n" if $debug;
next;
}
$seen{$dev . $ino} = $path;
if (-d _) {
if ($singledevice && $dev != $dirdev) {
print STDERR "Skipping $path (different device)\n" if $debug;
next;
}
process_dir ($path, \@fileinfo);
if ($subdirsize) {
$dirsize{$dir} += $dirsize{$path};
}
}
else {
# we usually care more about mtime, but when copying an old file where the mtime is preserved,
# the ctime might tell us that the file was recently created
# (I assume this is only the case when copying from a CD or DOS FAT file system,
# but it's still proven useful)
my $age = $now - ($mtime > $ctime ? $mtime : $ctime);
if (!defined($maxage) || $age < $maxage) {
print STDERR $path . "\n" if $debug;
# add the files to the file list if they're above the minimum required size
if ($size > $minsize) {
push @$fileinforef, [$path, $size];
push @{$dirfileinfo{$dir}}, [$path, $size];
}
# ...but always add them to the directory total (we filter the directories at the end of the loop)
$dirsize{$dir} += $size;
}
else {
print STDERR "Skipping $path ($age seconds < $maxage seconds)\n" if $debug;
}
}
}
}
else {
print STDERR "Can't open $dir: $!\n";
}
closedir($dirhandle);
}
# given a size in bytes and a unit, return a string describing that size in those units
# numbers are rounded down to int on conversion, so ensure you're using an appropriate unit
# (1048576, "M") -> "1 M"
# (1048576, "B") -> "1048576 B";
# (1048576, undef) -> "1048576"
# (1000000, "M") -> "0 M" # (caller should have used "K" :-))
sub bytes_to_units
{
my ($size, $units) = @_;
if (!$units) {
return $size;
}
my $divider;
if ($units eq "B") {
$divider = 1;
}
elsif ($units eq "K" || $units eq "KB") {
$divider = 1024;
}
elsif ($units eq "M" || $units eq "MB") {
$divider = (1024 * 1024);
}
elsif ($units eq "G" || $units eq "GB") {
$divider = (1024 * 1024 * 1024);
}
elsif ($units eq "T" || $units eq "TB") {
$divider = (1024 * 1024 * 1024 * 1024);
}
return int($size / $divider) . " " . $units;
}
# given a size and the units that size is in, return that size in bytes
# (1, "MB") => 1048576
# (1, "M") => 1048576
# (1048576) => 1048576
sub units_to_bytes
{
my ($size, $units) = @_;
if (!$units) {
return $size;
}
if (!defined($size)) {
$size = 1;
}
# KB==K, MB=M, etc.
$units =~ s/B$//;
if ($units eq "") {
}
elsif ($units eq "K") {
$size *= 1024;
}
elsif ($units eq "M") {
$size *= (1024 * 1024);
}
elsif ($units eq "G") {
$size *= (1024 * 1024 * 1024);
}
elsif ($units eq "T") {
$size *= (1024 * 1024 * 1024 * 1024);
}
else {
print STDERR "Unrecognised size unit $sizeunit\n";
return undef;
}
return $size;
}
# given a time and the units the time is in, return the number of seconds it equals
# (1, "s") -> 1
# (1, "h") -> 3600
# (2, "h") -> 7200
# (undef, "h") -> 3600
sub units_to_seconds
{
my ($time, $units) = @_;
if (!defined($time)) {
$time = 1;
}
if ($units eq "s") {
# no need to multiply by one
}
elsif ($units eq "m") {
$time *= 60;
}
elsif ($units eq "h") {
$time *= (60 * 60);
}
elsif ($units eq "d") {
$time *= (60 * 60 * 24);
}
elsif ($units eq "w") {
$time *= (60 * 60 * 24 * 7);
}
elsif ($units eq "l") {
$time *= (60 * 60 * 24 * 30);
}
elsif ($units eq "q") {
$time *= (60 * 60 * 24 * 90);
}
elsif ($units eq "y") {
$time *= (60 * 60 * 24 * 365);
}
else {
print STDERR "Unrecognised time unit $units\n";
return undef;
}
return $time;
}
sub print_file
{
my ($path, $size) = @_;
if (defined($pathwidth)) {
printf "%-${pathwidth}s ", $path;
}
else {
print "$path\t";
}
$size = bytes_to_units($size, $sizeunits);
print $size;
print "\n";
}
if (defined($maxage)) {
print STDERR "Reporting on files and directories no more than $maxage old\n";
}
else {
print STDERR "Reporting on all files and directories, irrespective of last modified time\n";
}
print STDERR "Reporting on files and directories at least $minsize in size\n";
if ($quiet) {
# -q is not recommended, use sudo or look at a directory you're allowed to look at
print STDERR "Warning: -q option used, results may be incomplete\n";
}
if ($maxage =~ /(\d*)(\D+)/) {
$maxage = units_to_seconds($1, $2);
}
if ($minsize =~ /(\d*)([A-Za-z]+)$/) {
$minsize = units_to_bytes($1, $2);
}
# TODO get file system device id, skip files on other devices
process_dir($root, \@fileinfo);
# create a list of directories and their sizes analagous to @fileinfo
if ($dirsonly || $groupbydir) {
foreach my $dir (keys %dirsize) {
if ($dirsize{$dir} > $minsize) {
push @dirinfo, [$dir, $dirsize{$dir}];
}
}
}
# include the directory totals in the file list
if ($showdirtotals) {
foreach my $dir (keys %dirsize) {
if ($dirsize{$dir} > $minsize) {
push @fileinfo, [$dir, $dirsize{$dir}];
}
}
}
# XXX free up some memory
%dirsize = {};
# $width means total width and we have to leave some for non-pathname fields (i.e. size)
if (defined($width)) {
$pathwidth = $width - 16;
}
# determine the maximum size required to display all paths
# (no need to allow space for non-pathname fields in this branch since we're using as much space as we need)
else {
# consider file names if we're printing them
if (! $dirsonly) {
foreach my $fileinfo (@fileinfo) {
my $path = ${$fileinfo}[0];
my $pathlength = length($path);
if ($pathlength > $pathwidth) {
$pathwidth = $pathlength;
}
}
}
# consider directory names if we're printing them
if ($showdirtotals || $dirsonly || $groupbydir) {
foreach my $dirinfo (@dirinfo) {
my $path = ${$dirinfo}[0];
my $pathlength = length($path);
if ($pathlength > $pathwidth) {
$pathwidth = $pathlength;
}
}
}
}
# print biggest directory first, followed by a sorted list of the biggest recent files in it...
if ($groupbydir || $dirsonly) {
my @sorteddirinfo = sort { ${$b}[1] <=> ${$a}[1] || ${$a}[0] cmp ${$b}[0] } @dirinfo;
foreach my $dirinfo (@sorteddirinfo) {
my $dir = ${$dirinfo}[0];
my $size = ${$dirinfo}[1];
print_file($dir, $size);
# only if we're printing the names of the big files in the directory as well
# (i.e. $groupbydir mode)
if (! $dirsonly) {
my @dirfileinfo = @{$dirfileinfo{$dir}};
my @sorteddirfileinfo = sort { ${$b}[1] <=> ${$a}[1] || ${$a}[0] cmp ${$b}[0] } @dirfileinfo;
foreach my $fileinfo (@sorteddirfileinfo) {
my $file = ${$fileinfo}[0];
my $size = ${$fileinfo}[1];
print_file($file, $size);
}
}
}
}
# print a flat list of all files below $root, sorted from largest to smallest...
else {
# sort on size, descending, sort by alphabetical if the size is the same
# (the alpha sort should ensure that directories are listed before files within them)
my @sortedfileinfo = sort { ${$b}[1] <=> ${$a}[1] || ${$a}[0] cmp ${$b}[0] } @fileinfo;
foreach my $fileinfo (@sortedfileinfo) {
my $file = ${$fileinfo}[0];
my $size = ${$fileinfo}[1];
print_file($file, $size);
}
}
# vi: set sw=4 ts=4 noet tw=0: