88

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

jww
  • 12,722
Eric V
  • 983

10 Answers10

133

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
  • 1,431
  • 2
  • 9
  • 2
48

From any directory:

du -a 2>/dev/null | sort -n -r

44

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 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
Welz
  • 207
Marcin
  • 868
10

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
  • 201
3

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
  • 39
  • 1
3

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
  • 311
1

Use

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

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

0

Use du. Try this to order the result:

du | sort -n
Heisenbug
  • 703
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
  • 26,615
-5
du -sh /path * | sort -nr | grep G

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

Kevin Panko
  • 7,466
hutch
  • 1