Quick-note: Some useful combinations for disk usage sorting in the command line
Context
The du -h
command shows disk usage in human-readable format. I usually use it as du -hs
to get the total disk usage in a specific directory.
But there are some useful combinations I usually forget. Thus this note.
1. Sort by size (largest first)
du -h | sort -hr
This will output a list of all dirs , sorted by size, largest first. Example:
❯ du -h | sort -hr 156M . 106M ./.venv 79M ./.venv/lib/python3.13/site-packages 79M ./.venv/lib/python3.13 79M ./.venv/lib 33M ./.venv/lib/python3.13/site-packages/babel 32M ./.venv/lib/python3.13/site-packages/babel/locale-data ... 12M ./.venv/lib/python3.13/site-packages/pip 12M ./.venv/lib64/python3.13/site-packages/lxml 9.9M ./.venv/lib/python3.13/site-packages/setuptools 9.2M ./.venv/lib/python3.13/site-packages/pygments 8.3M ./.venv/lib/python3.13/site-packages/pip/_vendor 7.9M ./.venv/lib/python3.13/site-packages/pygments/lexers 7.5M ./.venv/lib64/python3.13/site-packages/pillow.libs 6.8M ./.venv/lib64/python3.13/site-packages/PIL 5.5M ./nikola 4.8M ./npm_assets/node_modules/bootstrap
2. Sort by size (smallest first)
du -h | sort -h
This will output a list of all dirs , sorted by size, smallest first. Example:
❯ du -h | sort -h 4.0K ./.git/objects/info 4.0K ./.git/refs/tags 4.0K ./nikola/data/themes/bootblog4-jinja/assets/css 8.0K ./.github/workflows 8.0K ./.git/info ... 8.0K ./.git/refs/heads 8.0K ./.idea/inspectionProfiles 8.0K ./nikola-baseline-build/cache/galleries/demo 8.0K ./nikola-baseline-build/cache/posts 8.0K ./nikola-baseline-build/files/images 8.0K ./nikola-baseline-build/listings
3. Get the top 10 largest directories
du -h | sort -hr | head -10
This will output a list of the top 10 largest dirs, sorted by size, largest first. Example:
❯ du -h | sort -hr | head -10 156M . 106M ./.venv 79M ./.venv/lib/python3.13/site-packages 79M ./.venv/lib/python3.13 79M ./.venv/lib 33M ./.venv/lib/python3.13/site-packages/babel 32M ./.venv/lib/python3.13/site-packages/babel/locale-data 27M ./.venv/lib64/python3.13/site-packages 27M ./.venv/lib64/python3.13 27M ./.venv/lib64
4. For the current directory only (not subdirectories)
du -sh * | sort -hr
This will output a list of all the dirs in the current dir, sorted by size, largest first. Example:
❯ du -sh * | sort -hr 16M npm_assets 5.5M nikola 4.2M nikola-baseline-build 664K docs 512K tests 280K translations 120K CHANGES.txt 108K Nikola.egg-info 96K scripts 56K du-demo.cast 44K logo 8.0K CONTRIBUTING.rst 8.0K AUTHORS.txt 4.0K snapcraft.yaml 4.0K setup.py 4.0K setup.cfg 4.0K README.rst 4.0K pyproject.toml 4.0K MANIFEST.in 4.0K LICENSE.txt 4.0K dodo.py 4.0K CODE_OF_CONDUCT.md
5. Include hidden files/directories
du -sh .[^.]* * 2>/dev/null | sort -hr
Same as "4." but including hidden files and dirs
❯ du -sh .[^.]* * 2>/dev/null | sort -hr 106M .venv 24M .git 16M npm_assets 5.5M nikola 4.2M nikola-baseline-build 664K docs 512K tests 280K translations 120K CHANGES.txt 108K Nikola.egg-info 96K scripts 44K logo 36K .idea 32K .github 12K .pypt 8.0K .tx 8.0K CONTRIBUTING.rst 8.0K AUTHORS.txt 4.0K snapcraft.yaml 4.0K setup.py 4.0K setup.cfg 4.0K .readthedocs.yaml 4.0K README.rst 4.0K pyproject.toml 4.0K MANIFEST.in 4.0K LICENSE.txt 4.0K .gitignore 4.0K .gitattributes 4.0K .editorconfig 4.0K dodo.py 4.0K .coveragerc 4.0K CODE_OF_CONDUCT.md
6. Sort specific directory
du -h /path/to/directory | sort -hr
This will output a list of dirs, sorted by size, largest first, for a specific dir. Example:
❯ du -h nikola | sort -hr 5.5M nikola 2.4M nikola/data 2.0M nikola/plugins 1.4M nikola/data/themes 1.1M nikola/data/samplesite 656K nikola/plugins/command 568K nikola/data/themes/base 524K nikola/plugins/task 492K nikola/__pycache__ 436K nikola/data/samplesite/files 392K nikola/plugins/compile 256K nikola/plugins/shortcode
7. One-level deep only
du -h --max-depth=1 | sort -hr
This will output a list of dirs, sorted by size, largest first, for a specific depth. Example:
❯ du -h --max-depth=1 | sort -hr 156M . 106M ./.venv 24M ./.git 16M ./npm_assets 5.5M ./nikola 4.2M ./nikola-baseline-build 664K ./docs 512K ./tests 280K ./translations 108K ./Nikola.egg-info 96K ./scripts 44K ./logo 36K ./.idea 32K ./.github 12K ./.pypt 8.0K ./.tx # You can also change the max depth. ❯ du -h --max-depth=2 | sort -hr 156M . 106M ./.venv 79M ./.venv/lib 27M ./.venv/lib64 24M ./.git/objects 24M ./.git 16M ./npm_assets/node_modules 16M ./npm_assets 5.5M ./nikola 4.2M ./nikola-baseline-build 2.4M ./nikola/data 2.4M ./nikola-baseline-build/output 2.0M ./nikola/plugins
The flags
The -r
flag reverses the sort order, and the -h
flag in sort tells it to understand human-readable sizes (like 1K, 2M, 3G).