Tip: How to sort folders by size with one command line in Linux
I often need to find out what are the biggest folders from the space point of view. Take for example, I need to get the users that are using the most space on the system that gets close to filling up the hard drive. If we consider that the entire user’s data is under /home, I need to have a list of all the subfolders sorted by the size of the subfolders.
This can be achieved in many was on Linux, and for example if you have quota enabled it can be as easy as checking the quota list. But if quota is not enabled on the partition, I am using the following simple command to get the list of subfolders sorted by their size:
du --max-depth=1 /home/ | sort -n -r
If this is really big, as it often is, you might want to direct the output of the command to a file, and check back later in a few minutes when it is finished. ![]()
The result of this will include also the top level folder, and will look like this:
238208 /home/
164340 /home/marius
9324 /home/users
3660 /home/shared
32 /home/admin
...
Other ways I use du:
du -H --max-depth=1 /home/user
where I included “-H” to produce human readable format sizes (like: 1K 101M 2G)
>







3rd July 2006, 12:54
[...] Tip: How to sort folders by size with one command line in Linux | MDLog:/sysadmin [...]
18th July 2006, 15:27
I usually use something similar, but smaller to type:
du -sc /* | sort -nr
It is important to have the * because of the -s flag.
But remember that with this construction you miss the hidden files (files starting with a dot), because the shell-expansion of * doesn’t catch them.
20th July 2006, 22:13
I use this so often I stuck it in a shell script a couple years ago (could be improved):
—–
dutop.sh
—–
#!/bin/sh
dudir=”.”
topn=”10″
if [ "$1" ]; then
topn=”$1″
fi
if [ "$2" ]; then
dudir=”$2″
fi
du –max-depth 1 “$dudir” | sort -rn | head -”$topn”
7th December 2006, 03:39
du -sh * |grep G or du -sh * |grep M gives fast result
Jason.
http://linux-tweaks.blogspot.com
13th May 2007, 12:54
I’d been wondering how to get a sorted, human readable listing with du, and finally figured out a fairly short way to do it:
du -s /* | sort -n | cut -f 2- | while read a; do du -sh “$a”; done
This prints a sorted list in human-readable format, only problem with it is that the du is essentially done twice. That is not a major problem however as caching makes the second run really fast.
15th October 2007, 19:34
this is a one-liner that will show size for all folders inside current folder, including hidden ones, sorted and “human-readable”. For being a one-liner it’s kind of long, so I put it in a script file,
——-
duall
——
#!/bin/bash
ls -laQ | grep -r ‘^d’ | grep -v ‘\ \”\.\”$’ | grep -v ‘\ \”\.\.\”$’ | sed -r ’s/.*\ \”(.*)\”$/\1/’ | while read a; do du -s “$a”; done | sort -n | sed -r ’s/^([0-9]{1,4})\t(.*)/ \1\t\2/’ | sed -r ’s/^([\ 0-9]{4,5})\t(.*)/ \1\t\2/’ | sed -r ’s/^([\ 0-9]{6})\t(.*)/ \1\t\2/’ | sed -r ’s/^([\ 0-9])([\ 0-9]{3})([\ 0-9]{3})\t(.*)/\1 \2 \3\t\4/’
2nd November 2007, 01:45
Bang! This one liner bombs “syntax error near unexpected token”
I think I’ll patch sort to understand ‘10.2G’ etc.
2nd November 2007, 01:57
All the single quote and double quote characters are turned into some wierd unicode nastiness. Changing them to standard characters makes the script work. The output format is pretty nasty but never mind - I just found a hidden 8GB directory that’s not needed! Thanks!
16th November 2007, 17:52
du -s */ .[^.]*/ | sort -nr
is a good way to get the hidden directories as well. If you like the outrageously nice output format Kunchok called “pretty nasty,” then here’s an optimized version of the above “one-liner”:
#!/bin/bash
if [ "a$1" != "a" ]; then
cd $1
fi
du -s */ .[^.]*/ | sort -n | while read a; do echo ” $a”; done | sed -r ’s/^ *([ 0-9]{3})([ 0-9]{3})([ 0-9]{3}) *(.*)\/$/\1 \2 \3 \4/’
11th February 2008, 22:33
When running the script above I received “Syntax error: “(” unexpected”.
Here is an edit that works for me with a couple additions.
#!/bin/bash
if [ “a$1″ != “a†]; then
cd $1
fi
du –si */ .[^.]*/ | sort -n | while read a; do echo †$aâ€; done | sed -r ’s/^\ *([\ 0-9]{3})([\ 0-9]{3})([ 0-9]{3})\ *(.*)\/$/\1 \2 \3 \4/’ | egrep -v “k\ “
4th December 2008, 18:49
A good and short one liner using xargs
du -s ./* | sort -n| cut -f 2-|xargs -i du -sh {}
This will sort the folders according to size and display them in human readable format… to reverse the list
sue sort -nr option…