Script to find largest files

G

groups.user

Hi Script Gurus..

i'm looking for a script to find the largest files in a filesystem,
ordered by size.

Does anyone have a similar script out there which they can share ?

Thanks
 
M

Martijn Lievaart

du -s /* | sort -rn

Won't work, will not find hidden files. I normaly use (from memory):

find / -mindepth 1 -maxdepth 1 -print0 | xargs -0 du -s | sort -rn

M4
 
P

Peter J. Holzer

Won't work, will not find hidden files. I normaly use (from memory):

find / -mindepth 1 -maxdepth 1 -print0 | xargs -0 du -s | sort -rn

Why are you all intent on using du? It does something completely
different:

% find / -mindepth 1 -maxdepth 1 -print0 | xargs -0 du -s | sort -rn
6882520 /home
2778612 /usr
1021384 /var
134588 /lib
26692 /boot
[...]

/home is not the largest file on my root filesystem. In fact it isn't on
my root filesystem at all.

The correct solution is:

find $mountpoint -xdev -printf "%s %p\n" | sort -rn

(-printf is an extension of GNU find, but so is -print0. You may also
throw a -type f in there if you are only interested in regular files)

Since this is a perl group, not a shell group, here's the equivalent
script in perl:

#!/usr/bin/perl
use warnings;
use strict;

use File::Find;

my %files;

find(\&wanted, $ARGV[0]);
for (sort { $files{$b} <=> $files{$a} } keys %files) {
print "$files{$_} $_\n";
}

sub wanted {
my ($dev, $ino, $mode, $nlink, $uid, $gid, $rdev, $size) = lstat($_);
if ($dev != $File::Find::topdev) {
$File::Find::prune = 1;
return;
}
$files{$File::Find::name} = $size;
}
__END__

Which is quite a bit larger but should be OS independent and even deals
correctly with filenames with embedded newlines.

hp
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,045
Latest member
DRCM

Latest Threads

Top