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 users 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)

comments powered by Disqus