Reese Knowledgebase

Search for largest files in a file system

View Kristian Reese's profile on LinkedIn


If you like this article, please +1 or Recommend via FB with the provided buttons above:

Article ID: 1
by: Reese K.
Posted: 08 Apr, 2011
Last updated: 31 May, 2013
Views: 2048

This command will search for files 5 MB or larger, then list its size and location from smallest to largest in size.  Replace the mount point with which ever mount you want to look in, hence the -mount option to tell find not to descend directories on other filesystems.

find / -mount -type f -size +5096k -exec ls -lh {} \; | awk '{ print $5 ": " $9 }' | sort -n


Another cool thing to do is to find files based on a certain time frame and perform some action.  For example, if a file system is filling up due to a bunch of large log files, and the option to delete them is not available, search for and zip them up:

find * -type f -mtime +4 -print -exec /usr/local/bin/gzip --best {} \;

or look for files older than 7 days that are NOT named *.gz and zip them the up:

find . -mtime +7 ! -name "*.gz" -exec gzip {} \;


To apply tests only at certain depths, the depth, mindepth, and maxdepth can be used to aid in finding directories that are consuming the most space.

find /var -depth -mindepth 1 -maxdepth 3 -type d -exec du -sh {} \;

Once a directory has been identified, files older than 7 days can be deleted:

find /var/log -type f -mtime +7 -delete


Another option I've used in the past is to look at all the files, but is not as efficient as those above:

find [filesystem] -xdev -ls | awk '{ print $7, $11 }' | sort -nr | more


AIX

Seems the -mount option is not available to AIX.  In reading the man page, -xdev functions as such:

       -xdev
            Always evaluates to the value True. Prevents the find command from traversing a file system different from the one specified by the Path
            parameter

# find / -xdev -type f -size +2048k -print | xargs du -ka | sort -rn

# find / -xdev -type f -size +2048k -exec du -ka {} \; | sort -rn

PERL

#!/usr/local/bin/perl
#
# Lists all file in file systems sorted descending by size so that
# huge files show at the beginning.
#
open(IN,"/usr/bin/find / -xdev |") ||
        die "Couldn't do find . -xdev: $!\n";
open(OUT,"| /bin/sort -nr") ||
        die "Couldn't do sort -nr: $!\n";
while(<IN>) {
        chomp;
        $FileSize = (stat($_))[7];
        print OUT "$FileSize\t$_\n";
}
close(IN);
close(OUT);


This article was:   Helpful | Not Helpful
Also listed in
folder UNIX -> Linux

Prev   Next
     Redirecting standard output and standard error

RSS