How to find out the top space-consuming directories or files?

11

6

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?

Deqing

Posted 2013-01-08T09:58:26.827

Reputation: 4 107

Answers

5

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

Uwe Plonus

Posted 2013-01-08T09:58:26.827

Reputation: 1 354

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.

Rob

Posted 2013-01-08T09:58:26.827

Reputation: 2 152

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.

mnmnc

Posted 2013-01-08T09:58:26.827

Reputation: 3 637

9

du | sort -rn | head

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

Julien

Posted 2013-01-08T09:58:26.827

Reputation: 91

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.

Stefan Seidel

Posted 2013-01-08T09:58:26.827

Reputation: 8 812

2on the command line, you should try ncdu. It's an ncurses du, basically. – Rob – 2013-01-15T16:35:10.690

1

Try this:

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

Rajeev

Posted 2013-01-08T09:58:26.827

Reputation: 11

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.

Rich Homolka

Posted 2013-01-08T09:58:26.827

Reputation: 27 121

-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.

user275234

Posted 2013-01-08T09:58:26.827

Reputation: 1

3Can you expand upon your answer? – 50-3 – 2013-11-19T10:42:54.853