Operating System - HP-UX
1847661 Members
4527 Online
110265 Solutions
New Discussion

Finding largest file within a filesystem/disk

 
SOLVED
Go to solution
Chern Jian Leaw
Regular Advisor

Finding largest file within a filesystem/disk

HI

Is there a command in UNIX which allows us to retrive the LARGEST file/directory within a filesystem/disk-partition?

Or if there aren't, then could someone an example of a script which actually searches efficiently through the filesystem and list the largest fille/dir?

Thanks.
7 REPLIES 7
Justo Exposito
Esteemed Contributor

Re: Finding largest file within a filesystem/disk

Hi Chern,

You can do it by find command with the -size flag:
-size n[c] True if the file is n blocks long (512 bytes
per block). If n is followed by a c, the
size is in bytes.

Take a look at the man page for find.

Regards,

Justo.
Help is a Beatiful word
Michael Tully
Honored Contributor
Solution

Re: Finding largest file within a filesystem/disk

Hi,

Here is a script that I find useful. Hope it helps.

#######################################
#!/bin/sh

if [ $# -eq 2 ]
then
find $1 -type f -xdev -size +$(expr "$2" \* 2) -exec ll -d {} \;
else
echo "Usage : ${0##*/} "
fi
#######################################

Use it like this:

./largefiles.sh /var 20000
Anyone for a Mutiny ?
Andreas Voss
Honored Contributor

Re: Finding largest file within a filesystem/disk

Hi,

with the find command you can do this ie:

find -size +1000000c | xargs ls -ld

which will show you files within that are greater the 1MB

Regards
Peter Kloetgen
Esteemed Contributor

Re: Finding largest file within a filesystem/disk

Hi Chern,

here my solution for you:

ls -Ral | grep -v total |grep -v '^d' | awk '{print $5,$9}' | sort -k1n

does the following:
--> makes a recursive listing of all contents under current position
--> gets rid of total entries
--> gets rid of all directories
--> filters out size of files
--> does a numeric sort, biggest files are last in the output.
--> output is only size and name of files


Allways stay on the bright side of life!

Peter
I'm learning here as well as helping
harry d brown jr
Honored Contributor

Re: Finding largest file within a filesystem/disk



find / -type f -size +1000000c -exec ls -ls {} \; | sort -rn | more

Reduce the size from a million (1000000) to less if you want the "base" search to look at smaller files.

Change the "/" to a "." if you want to "cd" into a directory and look for the largest file:

cd somedirectory
find . -type f -size +10000c -exec ls -ls {} \; | sort -rn | more

live free or die
harry
Live Free or Die
Bill Hassell
Honored Contributor

Re: Finding largest file within a filesystem/disk

It's important *NOT* to look for big files first! You may have a directory with 10,000 temp files that are all 10kb in size--no big files there, but that is 100 megs of junk!

Always look for big directories first! And here is the easiest way:

du -kx / | sort -rn > /tmp/du.slash

Go through each mountpoint, especially /var and /usr. The output of the command is a list of the biggest directories first (in Kbytes). Once you find a big directory that doesn't look right, see if there is a big subdirectory inside where most of the space is used. At that point, use ll to locate the big files like this:

ll | sort -rnk5 | more

Please do not remove any big files until you determine why the file exists. New sysadmins will see the vmunix files and figure these are unrelated to HP-UX...


Bill Hassell, sysadmin
john korterman
Honored Contributor

Re: Finding largest file within a filesystem/disk

Hi Chern,

if you have a lot of cpu time and even more patience, you could try the attached script.


regards

John Korterman

it would be nice if you always got a second chance