How To Find Large Files and Directories in Unix

When you're trying to clean up your filesystems and reclaim some space, one of the first things you'll want to do is to confirm the largest directories and individual files you have. This can be easily done using two Unix commands: find command and du command.

Find files larger than a certain size

It's very simply to find files which are larger than a specified size. The find command accepts a size parameter, and you can specify the limits for file sizes in your command line.

This example finds all the files under /etc directory which are larger than 100k:

root@ubuntu# find /etc -size +100k
/etc/ssh/moduli
/etc/ssl/certs/ca-certificates.crt
/etc/bash_completion

If we look at their sizes, they really are above 100k:

root@ubuntu# ls -l /etc/ssh/moduli /etc/ssl/certs/ca-certificates.crt /etc/bash_completion
-rw-r--r-- 1 root root 215938 Apr 10  2007 /etc/bash_completion
-rw-r--r-- 1 root root 132777 Feb 19  2007 /etc/ssh/moduli
-rw-r--r-- 1 root root 149568 Sep  7  2007 /etc/ssl/certs/ca-certificates.crt

Find files within specified size limits

The real beauty of using find command is that you can specify both the lower and the upper file size limit in one command line. Working off the previous example, we can limit the search to find only files with the size of 100k-150k, quite easily:

root@ubuntu# find /etc -size +100k -size -150k
/etc/ssl/certs/ca-certificates.crt
/etc/bash_completion

As you can see from the syntax, the size specification can contain a sign – plus or minus indicates whether you're looking for a file with the size above or under a given figure.

Show directory sizes using du

du command takes a little while to run, depending on what directory you pass it as a parameter, but then prints you a list of all the subdirectories along with their sizes. Most common usage is shown below, -s parameter makes the command report a summary of disk usage stats for only the specified directories matching the /usr/* mask (and not their subdirectories), and -k specifies that we want to see the results in kilobytes:

greys@ubuntu$ du -sk /usr/*
4       /usr/X11R6
97664   /usr/bin
24      /usr/games
11628   /usr/include
167812  /usr/lib
0       /usr/lib64
96      /usr/local
25076   /usr/sbin
201500  /usr/share
4       /usr/src

In most Linux systems, this command had been updated to support a -h parameter, which makes sizes even easier to interpret:

greys@ubuntu$ du -sh /usr/*
4.0K    /usr/X11R6
96M     /usr/bin
24K     /usr/games
12M     /usr/include
164M    /usr/lib
0       /usr/lib64
96K     /usr/local
25M     /usr/sbin
197M    /usr/share
4.0K    /usr/src

Recommended books:

24 comments ↓

#1 Find Large Files in Unix | UNIX Tutorial: Learn UNIX on 03.24.08 at 5:01 pm

[...] RSS feed to get regular tips & tricks for all flavors of Unix. Thanks for visiting!I see that my Finding Large Files and Directories post is quite popular, yet there are a few more ways to simplify your search for the largest disk [...]

#2 UUIDs in Ubuntu | UNIX Tutorial: Learn UNIX on 05.12.08 at 1:50 pm

[...] Find large files and directories in Unix [...]

#3 Report System Resources Usage When Running Unix commands | UNIX Tutorial: Learn UNIX on 08.21.08 at 10:09 am

[...] Finding large files and directories [...]

#4 Klaudio Zic on 09.07.08 at 5:59 pm

Thanks a lot, this is the most useful and simple tuition I could find. I went through many UNIX handbooks in the past but it is always cool to refresh.

#5 Gleb Reys on 09.07.08 at 11:15 pm

Glad to have helped you, Klaudio!

#6 vovaNux on 09.10.08 at 5:25 am

Thanks for a great tutorial! I am looking a solution how to display directory sizes in the same way: files can take a little space each other, but if there are 1 million small files in a folder? My question is how to list big folders?

#7 Gleb Reys on 09.11.08 at 9:51 pm

Hi,

Your best bet would be to do something like du -sk /* for starters, and then narrow it down by du -sk /usr/* or something like that.

#8 vovaNux on 09.16.08 at 6:47 am

Thank for your reply! The problem is that I have too many small temporary files in the folder I'm searching and du hangs while executing such a command.

#9 How To Syncronize Directories with Rsync | UNIX Tutorial: Learn UNIX on 10.02.08 at 5:46 pm

[...] Finding large files and directories Please share: These icons link to social bookmarking sites where readers can share and discover new web pages. [...]

#10 Dave on 10.07.08 at 2:52 am

How do you search for a directory that could be in any other directory?
For example I want to find all "backup" directories but am not sure in which directories they all got saved.

#11 Klaudio Zic aka Claudio Antonio Valentin Solis on 11.16.08 at 10:32 am

Your tuition helped me on my EEEPC, a modest monster machine that just needed some extra UNIX scripts to perform fine!

#12 Gleb Reys on 11.16.08 at 6:35 pm

A pleasure to be of some help!

#13 Ben Harris on 03.21.09 at 11:13 pm

Thanks very much. I couldn't remember what the du command was, and spent ages trying to find it again. Cheers :)

#14 Gleb Reys on 03.22.09 at 6:35 pm

Hi Ben,
Glad to have helped!

Gleb

#15 Andrei on 04.22.09 at 11:06 am

Please tell me how can i find the largest directory through a bash script who will sum the file in the folder(only the final directories). Thank you

#16 Gleb Reys on 05.09.09 at 1:55 am

Andrei, du -sk does exactly that, but you need to then sort the output so that it finds the largest.

#17 Rubo77 on 07.04.09 at 4:56 pm

thx,
i use this to find large files on my webserver

want to see the sizes directly and dont want to see files in the folder /download/, so i use:

find /var/www/ -type f -name "*" -size +100M -exec du -h '{}' \;|grep -v /download/

#18 bdk on 10.21.09 at 12:47 am

Find the sizes of only directories sorted by size:

find . -type d -print0 | xargs -0 -n1 du -sk | sort -rn > dir-sizes.txt&

If you just want the 50 or so largest add '| head -n50' before piping it to the output file. Sizes are in KB for ez sort and reading.

#19 branchenverzeichnis online on 10.24.10 at 10:55 am

What a nice post. I really love reading these types or articles. I can?t wait to see what others have to say.

#20 Detecting large files and directories in Ubuntu « developingElegance(); on 12.02.10 at 4:23 pm

[...] http://www.unixtutorial.org/2008/03/find-large-files-and-directories/ [...]

#21 carrieDMS on 01.27.11 at 7:35 am

du -h | sort -n -r | head -n 20

#22 Partha Datta on 05.05.11 at 7:48 am

For finding the directory size I use a small script.
for i in `ls -F | grep '/$'` ; do
du -sh `echo "$i"`
done

#23 UnixSapp on 11.02.11 at 2:30 pm

I am running this cleanup script to keep my Solaris drop box well, cleaned up.

#!/bin/ksh
#
userdir=/clocal/ftp/user

for i in `ls ${userdir}`
do
find ${userdir}/${i}/incoming -mtime +2 -type f -ls
find ${userdir}/${i}/incoming -mtime +2 -type f -exec rm {} \;
find ${userdir}/${i}/incoming -mtime +2 -type f -size +200557600c -ls
find ${userdir}/${i}/incoming -mtime +2 -type f -size +200557600c -exec rm {} \;
done

But, if I think it misses a lot of filenames with spaces in them (People t4ransferring DOS files). Any suggestions to catch those files as well?

Mike

#24 UnixSapp on 11.03.11 at 3:41 pm

Sorry. It's the mtime that is giving the issues. It is Nov 3 and it is now deleting files from Oct 31. I thought that mtime +2 would have deleted the files on Nov 1 or 2 at the latest?

Leave a Comment