Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

list directory ,sort biggest 5 files and delete them.

2 views
Skip to first unread message

rsy2003

unread,
Apr 27, 2010, 10:36:45 AM4/27/10
to
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

unread,
Apr 27, 2010, 2:15:04 PM4/27/10
to
In article
<19d69f0f-c337-47d9...@k36g2000yqn.googlegroups.com>,
rsy2003 <rsy...@gmail.com> wrote:

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

0 new messages