-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdu.pl
More file actions
executable file
·203 lines (172 loc) · 4.59 KB
/
du.pl
File metadata and controls
executable file
·203 lines (172 loc) · 4.59 KB
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
#!/usr/bin/perl -w
##############################################################################
#
# File: disk_usage.pl
#
# Function: Traverse a Directory and get the disk usage of
# items in the folder(s)
# additional functionality :
# threshold (only show items > threshold val)
# recurse from initial directory to show grand-total
# (TODO)
#
# the syntax is :
# ./du.pl [directory] -t=[threshold value] -r
#
# Author(s): Michael Jelks
#
# Copyright: Copyright (c) 2003 Michael Jelks
# All Rights Reserved.
#
# Source: Started anew.
#
# Notes:
#
# Change History:
# 10/15/03 Started source
#
#
##############################################################################
use strict;
#Unbuffers output- good for nph-scripts
$| = 1;
my $output;
my @total_list;
my $dir = $ARGV[0];
my $threshold = 0;
my $subtotal = 0;
my $total = 0;
#clear the screen for readability
system "clear";
#exit program if they don't enter a [valid] directory
if (!$dir) {
print "You need to supply a directory before we can process the folders\n";
exit; }
if (!-d $dir) {
print "\"$dir\" is not a valid directory - check to make sure it exists\n";
exit; }
#this adds a trailing slash - needed for du -ks later on...
if ($dir !~ /\/$/) {
$dir =~ s/(.+)/$1\//; }
# add threshold values if -t argument exists -
# otherwise use initialized value up top
if ($ARGV[1]) {
$threshold = $ARGV[1];
$threshold =~ s/-t=(.+)/$1/;
print "Checking for values >= " .$threshold."MB\n"; }
&process_header;
&process_directory($dir, \$subtotal);
# init the process by priming the @total_list array
# with the $dir input via ARGV
$total_list[0] = $dir;
&process_list(\@total_list,$subtotal,"subtotal");
#then get the grand total for the entire directory
@total_list = ();
$output = &command_line("du", "-ks" , "\"" . $dir . "\"");
push(@total_list,$output);
$total = &process_list(\@total_list,$total,"total");
print "\n\n\n";
exit;
##############################################################################
#
# SUBS
#
##############################################################################
sub command_line {
my ($command, $argument, $parameter) = @_;
my $system;
$system = `$command $argument $parameter`;
return $system;
}
sub process_directory {
my ($dir, $subtotal) = @_;
#first get a list of all files for the specified directory
my $output = &command_line("ls","-1F",$dir);
my $list = &parse_list($output);
my $list_tmp;
#then get the subtotal for any files that exceed the threshold value
foreach my $item (@$list) {
$output = &command_line("du", "-ks" , "\"" . $dir . $item . "\"");
$list_tmp = &parse_list($output);
$subtotal = &process_list($list_tmp,$subtotal,"");
}
}
sub parse_list {
my $output = shift;
my (@list,@new_list);
my $listing;
@list = split(/\n/,$output);
foreach $listing (@list)
{
# if it's not a symlink - push into new array
if ($listing !~ /.+\@$/)
{
#remove any funny stuff at the end - * / etc.
$listing =~ s/(.+)\W$/$1/;
push (@new_list, $listing);
}
}
#sort file listing case-insensitve - gives human alphabetical order...
@new_list = sort {lc($a) cmp lc($b)} @new_list;
return \@new_list;
}
sub process_list {
my ($list, $total, $type) = @_;
my ($item, $name, $size);
foreach $item (@$list)
{
#extract the byte count and file/dir name from the list
if ($item =~ /^(\d+)\s+(.+)/)
{
$size = $1;
$name = $2;
#format the output to 2 decimals - print only if threshold exceeded
$size = sprintf("%.2f", ($size/1024));
if ($type eq "total")
{
&process_footer;
$name = "Total for directory -> " . $name;
&process_items($name, $size);
}
elsif ($size >= $threshold)
{
&process_items($name, $size);
$total += $size;
}
}
}
return $total;
}
## FORMATTING PERL CONSTRUCTS ##
sub process_header {
format HEADER =
SIZE
Path/Filename Size (in MB)
------------------------------------------------------------------------------
.
&setHandle("HEADER");
write;
}
sub process_footer {
format FOOTER =
-------------------------------------------------------------------------------
.
&setHandle("FOOTER");
write;
}
sub process_items {
my ($name, $size) = @_;
format ITEMS =
@<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<@>>>>>>>>>>>>>>
$name, $size
.
&setHandle("ITEMS");
write;
}
## PERL FORMATTING MAGIC ##
sub setHandle {
my $handle = shift;
my $oldhandle = select STDOUT;
$~ = $handle;
select ($oldhandle);
}