Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

Finding biggest files

by Anonymous Monk
on Feb 28, 2000 at 22:51 UTC ( [id://4533]=perlquestion: print w/replies, xml ) Need Help??

Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

I often go over my allotted space for my home directory and get nasty e-mails from my sysadmin telling me to get my account below the specified limit. I was wondering how I could write a script to recurse subdirectories and find the largest files and then perhaps prompt me if I wanted to delete the file. TIA

Replies are listed 'Best First'.
Re: Finding biggest files
by japhy (Canon) on Mar 01, 2000 at 22:59 UTC
    I'd use the following:
    % find -ls | awk '{print $7,$11}' | sort -rn 3093645 ./.perl/OLD_SITE/SOAP.tar.gz 97879 ./humor/dork2.jpg 87223 ./tmp/hat_puzzle.jpg 76110 ./.bin/webwatch/.log 55080 ./documentation/PerlTK-reference.zip 43704 ./documentation/perldoc/perlsub.pod 39424 ./NROTCURPI/invite.doc 33042 ./humor/dork1.jpg [etc...]
    You may need to fiddle with awk somewhat there. For me, this ran in 1.11 seconds. Faster than anything you could muck up in Perl, I'm sure.
RE: Finding biggest files
by Anonymous Monk on Feb 28, 2000 at 23:40 UTC
    I know this is a perl site, but I think it's important to use the best tool for the job. For this, I think I'd skip using perl (gasp) and just do "find ~/ -size +1024k" This will give you all the files over 1MB. If you get too many results just bump it up to 2048, or 4096, whatever. It's how i find large files. You might also do a du -hs * from your home directory. It'll tell you which directory structures are using the most amount of space. This is helpfull when your problem is lots of little files (like a buildup of temp files) instead of a few large files.
      I totally agree with this comment. Perl is a great language, but it needn't be used for things which standard Unix tools can already do. Find has so many options, I'd suggest you read through the man page, and you'll probably find some combination of arguments there that'll do exactly what you want, such as
      find ~ -size +1024k -printf "%p: %s\n" -ok rm {} \;
Re: Finding biggest files
by Crulx (Monk) on Feb 29, 2000 at 14:40 UTC
    I agree with the posters above on using find. But of course, you want to run this program every once in a while and you haven't figured out how to do shell programming so you combine the two and put in the program in perl!
    #!/usr/bin/perl system(qq[find ~ -size +1024k -printf "%p: %s\\n" -ok rm \\;]) == 0 or die "System call failed: $?";
    Ahh, Sweet perl.
    ---
    Crulx
    crulx@iaxs.net
Re: Finding biggest files
by btrott (Parson) on Feb 28, 2000 at 23:19 UTC
    Should be pretty easy. Just use File::Find to recursively find all of the files beneath a certain directory, and in your handler just record the size of each file.

    Then when you've found all the files, sort them by filesize, then loop through them and prompt whether to delete each file (perhaps above a certain size threshold?).

    Read the docs for File::Find and for the file tests in perlman:perlfunc.

RE: Finding biggest files
by Anonymous Monk on Mar 01, 2000 at 23:23 UTC
    Ok, this is not quite the perl solution, but it will work. find . -name \*.txt -size +14k | xargs ls -l In fact, take a look at File::Find. You also want to use the stat() function. It will give you your size for your function. I have some File::Find scripts at http://www.brie.com/coinduperl/

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://4533]
Approved by root
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others studying the Monastery: (7)
As of 2024-04-19 12:18 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found