12

My disk is running out of space:

$ df -h
Filesystem      Size  Used Avail Use% Mounted on
...
/dev/sda7       803G  715G   48G  95% /home

I'd like to go through the whole home directory and find out the top 10 space-consuming directories or files, to decide whom to be removed.

du -sh * can see size of all subdirectories, but it only gets current folder. I might need to divide into many subdirs respectively and recursively to get who are the culprits I need.

Is there a good way to do this?

9 Answers 9

5

If you can use a GUI try Baobab. This is a graphical tool for analyzing hard disk space usage.

10

ncdu is a command line option that scans directories and lists disk usage from highest to lowest. It's my go to tool for this.

You will still have to dive in recursively this way, but it's a nice easy way to do it.

9

Use this command:

t=$(df|awk 'NR!=1{sum+=$2}END{print sum}');du / –exclude /proc –exclude /sys –max-depth=1|sed '$d'|sort -rn -k1 | awk -v t=$t 'OFMT="%d" {M=64; for (a=0;a<$1;a++){if (a>c){c=a}}br=a/c;b=M*br;for(x=0;x<b;x++) {printf "\033[1;31m" "|" "\033[0m"}print " "$2" "(a/t*100)"% total"}'

It will show you the sort of a graph in command line showing the directories that consume the most of the space in percentage.

If you want to analyze the /home only - you can specify this in the command like this:

t=$(df|awk 'NR!=1{sum+=$2}END{print sum}');du /home –exclude /proc –exclude /sys –max-depth=1|sed '$d'|sort -rn -k1 | awk -v t=$t 'OFMT="%d" {M=64; for (a=0;a<$1;a++){if (a>c){c=a}}br=a/c;b=M*br;for(x=0;x<b;x++) {printf "\033[1;31m" "|" "\033[0m"}print " "$2" "(a/t*100)"% total"}'

It will look like that: enter image description here

Taken from here.

9

du | sort -rn | head

  • du = Disk Usage
  • sort using numerical order, reverse
  • the ten first lines
4

+1 for Baobab. on the command line, I usually just use du -hcsx * or du -hcsx * | sort -h (needs a failry recent version of sort) and then check subdirectories as I go on.

Also, mc has a neat feature of calculating subdirectory sizes by pressing Ctrl+Space on top of it (press Ctrl+Space on top of .. to make it calculate the size for all subdirectories. Then you can select sort by size in the menu.

1
  • 2
    on the command line, you should try ncdu. It's an ncurses du, basically.
    – Rob
    Jan 15, 2013 at 16:35
1

Try this:

du -a /dir | sort -nr | cut -f2 | xargs du -sh | head -n 10
0

Another GUI favorite: kdirstat

It has nice drilldown capabilities, and and (extensible) tools menu to help you delete/compress your files.

I also have a command line script that I call dir_usage that I've uploaded to pastebin.

0

Below is the much more usable command that lists top largest folders in /home. You can replace /home with / also.

sudo du -a --max-depth=2 --human-readable --time --exclude=.* /home 2>/dev/null | sort --human-numeric-sort --reverse | head -n 15

The great thing about above command is that it will only recurse upto 2 levels so you eliminate a lot of noise.

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

The commandline above does the following (in order):

  • Find all files (not directories) within current folder
  • Display usage for each of them
  • Sort the results numerically
  • Use only the last 10 (highest usage) lines
  • Cut out the 10 filenames (first column is usage, second is filename)
  • Display usage for each filename (in human format)

This will result in a list of human-readable sizes of the largest 10 files within the current folder and subfolders.

1
  • 3
    Can you expand upon your answer?
    – 50-3
    Nov 19, 2013 at 10:42

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .