From charlesreid1

The du utility in Unix can be used to find the sizes of files.

If you want to create a list of directories, and sort it by size, it takes a little bit of work.

The du command normally prints out sizes in bytes. This is fine, and makes it easy to sort things in order of least to greatest - except that it is unreadable. Humans cannot easily tell the order of magnitude of the number.

So the du command includes a -h flag to print things in human-readable format, with a G for gigbyte, M for megabyte, and so on. However, this means things are no loner sorted.

Instead, we can list all the things in the current directory, and pass that list to xargs to pass to du, one by one. The du utility determines how large each one is and prints out a pair: first, du prints the size in bytes, then du prints the name of the thing whose size it measured.

This can be passed to sort, so that we get the list of things in sorted order - sorted by size. We have to specify the n flag to get sort to arrange things in numerical order, and not string order.

Here's what we have so far:

$ /bin/ls -1 | xargs -n1 du -s | sort -n

96	conf_2016-08-27
153356	mysql_2015-10-09
186884	logs_2016-09-19
206496	mysql_2017-02-13
208120	mysql_2017-02-27
208308	mysql_2017-02-20
209548	mysql_2017-03-13
447900	mysql_2017-06-12
449044	mysql_2017-06-19
450072	mysql_2017-04-17

...

But we can keep going, and pass that sorted, 2-column list to cut, to extract the second column (field 2, in cut's parlance). Do that with cut -f2 to grab column 2. The default delimiter is tab so no need to specify that, but we can use e.g. -d',' if we have commas.

That gives us a list of sorted folders, sorted in order from least to greatest. Now we pass that list to xargs to pass to the du command one at a time, and we get du output, for each item in the list, in order from smallest to largest (or largest to smallest, if you reverse the sort.)

Here's the one-liner, and the resulting output:

$ /bin/ls -1 | xargs -n1 du -s | sort -nr | cut -s -f2 | xargs du -hs

1.5G	charlesreid1_2017-07-11
1.4G	charlesreid1_2017-06-18
1.4G	charlesreid1_2017-04-30
1.2G	mysql_2017-04-01
1.2G	mysqlfull_2017-02-14
1.1G	mysqlfull_2016-09-18
1.1G	mysqlfull_2016-08-27
1.1G	mysql_2016-08-23
449M	mysql_2017-07-10
448M	mysql_2017-06-26
444M	mysql_2017-07-03
440M	mysql_2017-04-17
439M	mysql_2017-06-19
438M	mysql_2017-06-12
205M	mysql_2017-03-13
204M	mysql_2017-02-20
204M	mysql_2017-02-27
202M	mysql_2017-02-13
183M	logs_2016-09-19
150M	mysql_2015-10-09
1.9M	mediawiki
96K	conf_2016-08-27