How to find the largest directories or largest files?

34

20

Under Linux, I'm looking for a command to list the largest file and/or the largest directories under a directory.

Eric V

Posted 2011-04-28T12:56:58.667

Reputation: 443

Question was closed 2015-03-09T01:10:30.683

The most useful tool I've found is xdiskusage (http://xdiskusage.sourceforge.net) This shows graphically where the files are - by size. Great tool! (and it works directly with X11)

– jcoppens – 2015-04-20T21:31:07.207

How this is duplicated of some question which has been closed as off-topic? Doesn't make sense. – kenorb – 2015-04-20T22:08:39.897

Answers

17

From any directory:

du -a | sort -n -r

Brent Worden

Posted 2011-04-28T12:56:58.667

Reputation: 302

1this shows individual files, but the question is about directories as well – knocte – 2017-02-07T06:12:10.943

du with no arguments summarizes disk usage by directories. du with -a produces the same directory information and includes the disk usage for individual files as well. – Brent Worden – 2017-02-07T13:47:05.023

can't run sort when you're out of space :=( – Jonathan Henson – 2017-04-10T23:10:27.290

30

A utility called ncdu will give you the information you are looking for.

sudo apt-get install ncdu

On OS X, it can be installed using Homebrew:

brew install ncdu

David Pratt

Posted 2011-04-28T12:56:58.667

Reputation: 401

2+1 I'd never heard of this one before - worked great on the Mac too. – Andrew E – 2014-11-04T02:24:53.053

This is a much nicer solution than both of the higher answers. – AlexLordThorsen – 2015-07-22T21:21:44.073

This is mind-blowing. Suggest this over all other answers! – Allen Gingrich – 2015-08-07T20:32:11.073

28

Following command shows you one level of directories and their sizes

du --max-depth=1 /path | sort -r -k1,1n

If one of them really sticks out (the last one on the list is the largest due to sort -r), then you re-run the command on that directory, and then keep going until you find the offending directory / file.

If all you want is the ten biggest files just do

find /home -type f -exec du -s {} \; | sort -r -k1,1n | head

Marcin

Posted 2011-04-28T12:56:58.667

Reputation: 678

biggest number ends up at the bottom for me no matter if I add sort -r or not. Is there a way to get the biggest number at the top? – squarecandy – 2013-10-27T22:17:38.710

You must indicate to sort which column you want to sort by, and that it's numeric (not alphanumeric). That's what -k1,1rn would do. By default sort does uses alphanumeric sort on first column. – Marcin – 2013-10-28T12:45:23.160

Yes, it's sorting correctly with that, but it's in ascending order low to high numbers no matter if I include sort or sort -r. Am I misunderstanding how the -r works? I guess it's not a big deal. Your example is very helpful and got me the info I needed. – squarecandy – 2013-10-28T16:17:53.377

2With the sort I have (sort (GNU coreutils) 8.13 in Ubuntu 12.04.3) the option -r does not work if -n immediately follows -k (-k1,1n). This order of options works: sort -rnk1,1. – pabouk – 2013-12-01T08:26:42.533

4

du -sk * | sort -nr | head -1

This will show the biggest directory/file in a directory in KB. Changing the head value will result in the top x files/directories.

Sridharpp

Posted 2011-04-28T12:56:58.667

Reputation: 41

2

This post will help you well:

cd /path/to/some/where
du -a /var | sort -n -r | head -n 10
du -hsx * | sort -rh | head -10

Matz

Posted 2011-04-28T12:56:58.667

Reputation: 211

2

Following command will return top 10 biggest files from given /path

du -a -h /path | sort -h -r | head -n 10

I like to use -h options for readability. Both du and sort need to have -h.

Jiang

Posted 2011-04-28T12:56:58.667

Reputation: 121

1

Use

ls -A | xargs -I artifact du -ms artifact | sort -nr

Optionally, you can add a pipe and use head -5

Abhishek

Posted 2011-04-28T12:56:58.667

Reputation: 11

0

Use du. Try this to order the result:

du | sort -n

Heisenbug

Posted 2011-04-28T12:56:58.667

Reputation: 645

0

Try the following one-liner (displays top-20 biggest files in the current directory):

ls -1Rs | sed -e "s/^ *//" | grep "^[0-9]" | sort -nr | head -n20

or with human readable sizes:

ls -1Rhs | sed -e "s/^ *//" | grep "^[0-9]" | sort -hr | head -n20

The second command to work on OSX/BSD properly (as sort doesn't have -h), you need to install sort from coreutils.

So these aliases are useful to have in your rc files (every time when you need it):

alias big='du -ah . | sort -rh | head -20'
alias big-files='ls -1Rhs | sed -e "s/^ *//" | grep "^[0-9]" | sort -hr | head -n20'

kenorb

Posted 2011-04-28T12:56:58.667

Reputation: 16 795

-5

du -sh /path * | sort -nr | grep G

G for GIG (to weed out smaller) files/directories

hutch

Posted 2011-04-28T12:56:58.667

Reputation: 1

This lists all the files and folders, showing the size. It doesn't sort the size by the K, M or G's worth of bytes, unless you GREP it as you shown – Canadian Luke – 2013-10-15T17:31:33.083

Also it will find anything with a G in the file's name. – Kevin Panko – 2013-10-15T18:03:04.713