Sort Folders By Size

Posted on by  admin

Summary: Microsoft Scripting Guy, Ed Wilson, uses Windows PowerShell 3.0 to sort folders by size. Microsoft Scripting Guy, Ed Wilson, is here. It is amazing how things continue to go in circles I know I have written a script to sort folders by size many times in many different languages.

Let's say I want to get the size of each directory of a Linux file system. When I use ls -la I don't really get the summarized size of the folders.

If I use df I get the size of each mounted file system but that also doesn't help me. And with du I get the size of each subdirectory and the summary of the whole file system.

But I want to have only the summarized size of each directory within the ROOT folder of the file system. Is there any command to achieve that?

Kamil Maciorowski
31.1k16 gold badges62 silver badges91 bronze badges
2ndkauboy2ndkauboy
1,5884 gold badges11 silver badges8 bronze badges

migrated from stackoverflow.comJul 12 '10 at 18:25

This question came from our site for professional and enthusiast programmers.

9 Answers

This does what you're looking for:

What this means: Sims 4 cheat mods.

  • -s to give only the total for each command line argument.
  • -h for human-readable suffixes like M for megabytes and G for gigabytes (optional).
  • /* simply expands to all directories (and files) in /.

    Note: dotfiles are not included; run shopt -s dotglob to include those too.

Also useful is sorting by size:

Here:

Sort Folders By Size Windows 10

  • -h ensures that sort interprets the human-readable suffixes correctly.
ThomasThomasWindows sort folders by size
4,9051 gold badge13 silver badges13 bronze badges

I often need to find the biggest directories, so to get a sorted list containing the 20 biggest dirs I do this:

In this case the sizes will be reported in megabytes.

Janne Pikkarainen

Sort Folders By Size Powershell

Janne Pikkarainen

Windows 10 Sort By Size

6,4291 gold badge23 silver badges30 bronze badges

I like to use Ncdu for that, you can use the cursor to navigate and drill down through the directory structure it works really well.

BuddhaLincolnBuddhaLincoln
Folders

The existing answers are very helpful, maybe some beginner (like me) will find this helpful as well.

  1. Very basic loop, but for me this was a good start for some other size related operations:

  2. Very similar to the first answer and nearly the same result as 1.), but it took me some time to understand the difference of * to ./* if in a subdirectory:

Der Hochstapler
69.3k50 gold badges236 silver badges288 bronze badges
MartinMartin

The following du invocation should work on BSD systems:

PhilippPhilipp

This isn't easy. The du command either shows files and folders (default) or just the sizes of all items which you specify on the command line (option -s).

To get the largest items (files and folders), sorted, with human readable sizes on Linux:

This will bury you in a ton of small files. You can get rid of them with --threshold (1 MB in my example):

The advantage of this command is that it includes hidden dot folders (folders which start with .).

If you really just want the folders, you need to use find but this can be very, very slow since du will have to scan many folders several times:

Aaron DigullaAaron Digulla
4,5607 gold badges36 silver badges61 bronze badges

Be aware, that you can't compare directories with du on different systems/machines without getting sure, both share the same blocksize of the filesystem. This might count if you rsync some files from a linux machine to a nas and you want to compare the synced directory on your own. You might get different results with du because of different blocksizes..

Jimmy KoertingJimmy Koerting

You might also want to check out xdiskusage. Will give you the same information, but shown graphically, plus allows to drill down (very useful). There are other similar utilities for KDE and even Windows.

sleskesleske
18.3k8 gold badges53 silver badges83 bronze badges

You could use ls in conjunction with awk:

The output of ls is piped to awk. awk starts processing the data. Standard delimiter is space. The sum variable tot is initialised to zero; the following statement is executed for each row/line outputted by ls. It merely increments tot with the size. $5 stands for fifth column (outputted by ls). At the end we divide by (1024*1024) to sum in megabytes.

If you would convert this into a script or function (.bashrc) you can also use it to get the size of certain subsets of directories, according to filetypes.

If you want system wide information, kdirstat may came in handy!

J.M. StoorvogelJ.M. Stoorvogel

Not the answer you're looking for? Browse other questions tagged linuxunixconsole or ask your own question.

Comments are closed.