Top 10 files & directories by size

Add this alias line to your command line environment (.profile, .bashrc, …) to quickly see the total size and find and sort the top 10 largest files and directories in your current path. I like to keep things simple and don’t let it recurse directories, but of course you can easily adjust its behavior. :)

  1. Howto
  2. OSX Update 2013-07-12:
  3. Update 2025
  4. Synology DSM

Howto

Drop the sudo when you’re not an admin.

alias dud='sudo du -ahd 1 | sort -hr | head -n 11'
65M .
46M ./oud
17M ./flickr
1.3M ./Saree.jpg
1.2M ./Pinstripe.jpg
364K ./photos
136K ./error_log
80K ./wacu.jpg
28K ./wackywheels
24K ./spyro_avatar2.png
20K ./m
du -ahd 1Disk usage, -a is files and dirs, -h is human size, -d 1 is max depth 1.
sort -hrSort lines, -h parse human size, -r is reversed order.
head -n 11Only output the first 10 lines. (output includes grand total line)

I use this alias a lot on my Ubuntu and Debian servers. For Mac OSX I have not found a useful solution yet. The default command-line tools are very limited, its sort can’t parse human sizes.

OSX Update 2013-07-12:

After messing around with the OSX tools a bit more I decided to write my own script in PHP, which is properly installed by default. Just save the code from this gist (raw) to a file somewhere, make it executable with chmod +x theSavedFile and add to your .bash_profile:

alias dud='/path/to/theSavedFile'

Update 2025

When available for your OS, use the command ncdu instead. It is perfect and interactive! You can simply browse directories and it will instantly calculate and sort the used disk space.

# Mac with Homebrew
brew install ncdu

# Ubuntu / Debian
apt install ncdu

# RHEL
dnf install ncdu

Synology DSM

  1. Go to Package Center
  2. Add SynoCommunity source
  3. Community tab
  4. Install SynoCLI Disk Tools

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *