Discussion:
list directory ,sort biggest 5 files and delete them.
(too old to reply)
rsy2003
2010-04-27 14:36:45 UTC
Permalink
Hi perl gurus,
I'm new to perl but ok on unix. I have a problem as system admin.
I have to do this almost every week where i need to list directory
contents on d:\drive of windows server and get top 5 files, take their
size and delete them.

i did use commands available on dos and also used unix head and grep
utils and made to get 5 files.

i used dir sort with size, made unix head -5 and then put that to
file,but i want to delete those and also compute size of d drive.

eg:

d:\test1.txt - 5kb
test2.txt 4kb
test3.txt 3kb
....

how can i write all in perl only?
thanks for any help
jay
Jim Gibson
2010-04-27 18:15:04 UTC
Permalink
In article
Post by rsy2003
Hi perl gurus,
I'm new to perl but ok on unix. I have a problem as system admin.
I have to do this almost every week where i need to list directory
contents on d:\drive of windows server and get top 5 files, take their
size and delete them.
i did use commands available on dos and also used unix head and grep
utils and made to get 5 files.
i used dir sort with size, made unix head -5 and then put that to
file,but i want to delete those and also compute size of d drive.
d:\test1.txt - 5kb
test2.txt 4kb
test3.txt 3kb
....
how can i write all in perl only?
I would use the File::Find module and its find() function to traverse
the directory tree on the d: drive and the file test operator -s to get
the file size. For each file found, I would save the file size and name
in an array of arrays (untested):

push( @files, [ -s $_, $File::Find::name] );

I would then sort this array by file size and print or delete the first
5 elements.

for my $file ( sort { $b->[0] <=> $a->[0] } @files ) {
# do whatever to first n elements
}

(You may have to swap $a and $b in the sort block function to sort by
increasing or decreasing size.)

You can get more details with these commands:

perldoc File::Find
perldoc -f -s
perldoc -f sort

Good luck.
--
Jim Gibson
Loading...