Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I'm looking for a program to show me which files/directories occupy the most space, something like:

74% music
 \- 60% music1
 \- 14% music2
12% code
13% other

I know that it's possible in KDE3, but I'd rather not do that - KDE4 or command line are preferred.

share|improve this question

15 Answers 15

up vote 67 down vote accepted

To find the largest 10 files (linux/bash):

find . -type f -print0 | xargs -0 du | sort -n | tail -10 | cut -f2 | xargs -I{} du -sh {}

To find the largest 10 directories:

find . -type d -print0 | xargs -0 du | sort -n | tail -10 | cut -f2 | xargs -I{} du -sh {}

Only difference is -type {d:f}.

Handles files with spaces in the names, and produces human readable file sizes in the output. Largest file listed last. The argument to tail is the number of results you see (here the 10 largest).

There are two techniques used to handle spaces in file names. The find -print0 | xargs -0 uses null delimiters instead of spaces, and the second xargs -I{} uses newlines instead of spaces to terminate input items.

example:

$ find . -type f -print0 | xargs -0 du | sort -n | tail -10 | cut -f2 | xargs -I{} du -sh {}

  76M    ./snapshots/projects/weekly.1/onthisday/onthisday.tar.gz
  76M    ./snapshots/projects/weekly.2/onthisday/onthisday.tar.gz
  76M    ./snapshots/projects/weekly.3/onthisday/onthisday.tar.gz
  76M    ./tmp/projects/onthisday/onthisday.tar.gz
  114M   ./Dropbox/snapshots/weekly.tgz
  114M   ./Dropbox/snapshots/daily.tgz
  114M   ./Dropbox/snapshots/monthly.tgz
  117M   ./Calibre Library/Robert Martin/cc.mobi
  159M   ./.local/share/Trash/files/funky chicken.mpg
  346M   ./Downloads/The Walking Dead S02E02 ... (dutch subs nl).avi
share|improve this answer

I always use ncdu. It's interactive and very fast.

share|improve this answer
    
Yeah and it's small! –  Luke Stanley Jul 18 '11 at 0:40
4  
I love ncdu. It's one of my favorite google finds. –  Rob Jul 24 '12 at 18:33
2  
Wow. how did I not know this existed. Thanks! –  pixel Sep 11 '12 at 21:57
6  
+1000 for ncdu --- it's like htop for disk space. Super useful! –  Noah Sussman Jun 26 '13 at 1:27
    
since there do not seem to be flags nor a .config option, here's the key sequence you'll probably type every time you run it if you like seeing files and folders mingled and relative percentage stats: [t] [g] [g]. –  rymo Nov 10 '13 at 21:36

For a quick view:

du | sort -n

lists all directories with the largest last.

du --max-depth=1 * | sort -n

or, again, avoiding the redundant * :

du --max-depth=1 | sort -n

lists all the directories in the current directory with the largest last.

(-n parameter to sort is required so that the first field is sorted as a number rather than as text but this precludes using -h parameter to du as we need a significant number for the sort)

Other parameters to du are available if you want to follow symbolic links (default is not to follow symbolic links) or just show size of directory contents excluding subdirectories, for example. du can even include in the list the date and time when any file in the directory was last changed.

share|improve this answer
3  
Is the * really necessary. Doesn't it by default include all files in the current dir? –  David Pearce Jul 21 '09 at 17:02
    
No, the * should be redundant. I'm not sure whether using it is the sign of a good habit or a bad one. Thanks for pointing it out. I've amended the answer to reflect it as optional. –  mas Jul 22 '09 at 9:16
    
This is fine but the results aren't very friendly. I usually turn to this: find {/path/to/directory} -type f -size +{file-size-in-kb}k -exec ls -lh {} \; | awk '{ print $8 ": " $5 }' –  deed02392 Mar 6 '12 at 20:02
    
roman# du --max-depth=1 | sort -n du: illegal option -- - usage: du [-A] [-H | -L | -P] [-a | -s | -d depth] [-c] [-l] [-h | -k | -m | -B bsize] [-n] [-x] [-I mask] [file ...] –  holms Apr 6 '12 at 16:00

For most things, I prefer CLI tools, but for drive usage, I really like filelight. The presentation is more intuitive to me than any other space management tool I've seen.

alt text

share|improve this answer
1  
Filelight is my space-hog pruning tool of choice. –  Ryan Thompson Sep 11 '09 at 8:07
    
Very good app. +1 –  rpax Jul 1 at 17:43

Filelight is better for KDE users, but for completeness (question title is general) I must mention Baobab is included in Ubuntu, aka Disk Usage Analyzer:

enter image description here

share|improve this answer

A GUI tool, KDirStat, shows the data both in table form and graphically. You can see really quickly where most of the space is used.

enter image description here

I'm not sure if this is exactly the KDE tool you didn't want, but I think it still should be mentioned in a question like this. It's good and many people probably don't know it - I only learned about it recently myself.

share|improve this answer
    
Thanks for the answer. It's the exact same tool I had in KDE3, but I moved to KDE 4. –  Robert Munteanu Jul 22 '09 at 11:20
    
Are you sure you can't get kdirstat for KDE4? –  Jonik Jul 22 '09 at 11:44
3  
Kdirstat is sooooo slow. Use ncdu instead. –  Daenyth Jul 7 '10 at 15:31
    
I just hit ctrl+f to find ncdu, and saw that I've already upvoted @Daenyth –  Rob Feb 18 '13 at 5:06
    
On KDE, it's simply called k4dirstat. –  phihag Feb 26 at 14:07

A Combination is always the best trick on Unix.

du -sk $(find . -type d) | sort -n -k 1

Will show directory sizes in KB and sort to give the largest at the end.
Tree-view will however needs some more fu... is it really required?

Note that this scan is nested across directories so it will count sub-directories again for the higher directories and the base directory . will show up at the end as the total utilization sum.

You can however use a depth control on the find to search at a specific depth.
And, get a lot more involved with your scanning actually... depending on what you want. Depth control of find with -maxdepth and -mindepth can restrict to a specific sub-directory depth.


Here is a refined variation for your arg-too-long problem

find . -type d -exec du -sk {} \; |  sort -n -k 1
share|improve this answer
    
I tried that and i got lots of 'du: Task: No such file or directory' –  David Pearce Jul 21 '09 at 7:12
    
Thanks for the answer. Unfortunately I get bash: /usr/bin/du: Argument list too long –  Robert Munteanu Jul 21 '09 at 7:21

I like gt5. You can navigate the tree and open subdirectories to drill down for more detail. It uses a text-mode web browser, such as lynx, to display the results. Install elinks for best results.

alt text

share|improve this answer

Although it does not give you a nested output like that, try du

du -h /path/to/dir/

Running that on my Documents folder spits out the following:

josh-hunts-macbook:Documents joshhunt$ du -h
  0B    ./Adobe Scripts
  0B    ./Colloquy Transcripts
 23M    ./Electronic Arts/The Sims 3/Custom Music
  0B    ./Electronic Arts/The Sims 3/InstalledWorlds
364K    ./Electronic Arts/The Sims 3/Library
 77M    ./Electronic Arts/The Sims 3/Recorded Videos
101M    ./Electronic Arts/The Sims 3/Saves
 40M    ./Electronic Arts/The Sims 3/Screenshots
1.6M    ./Electronic Arts/The Sims 3/Thumbnails
387M    ./Electronic Arts/The Sims 3
387M    ./Electronic Arts
984K    ./English Advanced/Documents
1.8M    ./English Advanced
  0B    ./English Extension/Documents
212K    ./English Extension
100K    ./English Tutoring
5.6M    ./IPT/Multimedia Assessment Task
720K    ./IPT/Transaction Processing Systems
8.6M    ./IPT
1.5M    ./Job
432K    ./Legal Studies/Crime
8.0K    ./Legal Studies/Documents
144K    ./Legal Studies/Family/PDFs
692K    ./Legal Studies/Family
1.1M    ./Legal Studies
380K    ./Maths/Assessment Task 1
388K    ./Maths
[...]

Then you can sort the output by piping it through to sort

du /path/to/dir | sort -n
share|improve this answer
    
Thanks, but it does not properly show which directories are largest. If I start it in my home directory the output is unusable. –  Robert Munteanu Jul 21 '09 at 7:02

Here is the script which does it for you automatically.

http://www.thegeekscope.com/linux-script-to-find-largest-files/

Following is the sample output of the script:

**# sh get_largest_files.sh / 5**

[SIZE (BYTES)]     [% OF DISK] [OWNER]         [LAST MODIFIED ON]        [FILE] 

56421808           0%           root           2012-08-02 14:58:51       /usr/lib/locale/locale-archive
32464076           0%           root           2008-09-18 18:06:28       /usr/lib/libgcj.so.7rh.0.0
29147136           0%           root           2012-08-02 15:17:40       /var/lib/rpm/Packages
20278904           0%           root           2008-12-09 13:57:01       /usr/lib/xulrunner-1.9/libxul.so
16001944           0%           root           2012-08-02 15:02:36       /etc/selinux/targeted/modules/active/base.linked

Total disk size: 23792652288 Bytes
Total size occupied by these files: 154313868 Bytes  [ 0% of Total Disc Space  ]

*** Note: 0% represents less than 1% ***

You may find this script very handy and useful !

share|improve this answer
2  
While the linked website does give instructions, it is preferred for you to paraphrase then reference the external site (which looks like a personal blog anyways). This will prevent link rot and help more people on this site –  Canadian Luke Sep 6 '12 at 7:58

To find the top 25 files in the current directory and its subdirectories:

find . -type f -exec ls -al {} \; | sort -nr -k5 | head -n 25

The will output the top 25 files by sorting based on the size of the files via the "sort -nr -k5" piped command.

share|improve this answer
du -chs /*

Will show you a list of the root directory.

share|improve this answer

Although finding the percentage disk usage of each file/directory is beneficial, most of the time knowing largest files/directories inside the disk is sufficient.

So my favorite is this:

# du -a | sort -n -r | head -n 20

And output is like this:

28626644        .
28052128        ./www
28044812        ./www/vhosts
28017860        ./www/vhosts/example.com
23317776        ./www/vhosts/example.com/httpdocs
23295012        ./www/vhosts/example.com/httpdocs/myfolder
23271868        ./www/vhosts/example.com/httpdocs/myfolder/temp
11619576        ./www/vhosts/example.com/httpdocs/myfolder/temp/main
11590700        ./www/vhosts/example.com/httpdocs/myfolder/temp/main/user
11564748        ./www/vhosts/example.com/httpdocs/myfolder/temp/others
4699852         ./www/vhosts/example.com/stats
4479728         ./www/vhosts/example.com/stats/logs
4437900         ./www/vhosts/example.com/stats/logs/access_log.processed
401848          ./lib
323432          ./lib/mysql
246828          ./lib/mysql/mydatabase
215680          ./www/vhosts/example.com/stats/webstat
182364          ./www/vhosts/example.com/httpdocs/tmp/aaa.sql
181304          ./www/vhosts/example.com/httpdocs/tmp/bbb.sql
181144          ./www/vhosts/example.com/httpdocs/tmp/ccc.sql
share|improve this answer

To complete the list a little bit more, I add my favorite disk usage analyzer, which is xdiskusage.

The GUI remembers me of some other good ol' X utilities, it's fast and not bloated, but you can nevertheless navigate easily in the hierarchy and have some display options:

$ xdiskusage /usr

enter image description here

share|improve this answer

for listing out the largest files,

find . -type f -exec ls -s {} \; | sort -n -r

refer #9 here: find command examples

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.