73

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

3
  • 1
    The most useful tool I've found is xdiskusage (xdiskusage.sourceforge.net) This shows graphically where the files are - by size. Great tool! (and it works directly with X11)
    – jcoppens
    Apr 20, 2015 at 21:31
  • How this is duplicated of some question which has been closed as off-topic? Doesn't make sense.
    – kenorb
    Apr 20, 2015 at 22:08
  • @jcoppens Please post an answer, the tool is very good.
    – Iacchus
    Nov 1, 2021 at 20:30

10 Answers 10

37

From any directory:

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

5
  • 4
    this shows individual files, but the question is about directories as well
    – knocte
    Feb 7, 2017 at 6:12
  • 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. Feb 7, 2017 at 13:47
  • 2
    can't run sort when you're out of space :=( Apr 10, 2017 at 23:10
  • This prints a lot of du: cannot read directory './snap/core20/2015/var/cache/ldconfig': Permission denied when ran from root dir Sep 22, 2023 at 11:22
  • You can redirect stderr to /dev/null to avoid those errors. stackoverflow.com/a/44758924/633150 Sep 22, 2023 at 12:22
98

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
7
  • 3
    +1 I'd never heard of this one before - worked great on the Mac too.
    – Andrew E
    Nov 4, 2014 at 2:24
  • 1
    This is a much nicer solution than both of the higher answers. Jul 22, 2015 at 21:21
  • 2
    This is mind-blowing. Suggest this over all other answers! Aug 7, 2015 at 20:32
  • Fyi, this is an ncurses (text-based UI) application. So, no need to extensively discuss CLI parameters and whatnot; just install it and execute ncdu in the directory you'd like to analyze.
    – rinogo
    Dec 1, 2020 at 18:19
  • On CentOS, you can use this to install a repo containing ncdu: sudo yum install epel-release
    – Rocket
    Apr 6, 2022 at 17:20
42

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
5
  • 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? Oct 27, 2013 at 22:17
  • 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
    Oct 28, 2013 at 12:45
  • 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. Oct 28, 2013 at 16:17
  • 3
    With 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. Dec 1, 2013 at 8:26
  • Great answer. On OS X the option is -d 1 instead of --max-depth 1. What is also useful is -m which then reports in megabyte instead of block count.
    – freespace
    Apr 18, 2021 at 11:32
8

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.

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.

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
0
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
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'
-5
du -sh /path * | sort -nr | grep G

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

2
  • 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 Oct 15, 2013 at 17:31
  • Also it will find anything with a G in the file's name. Oct 15, 2013 at 18:03

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