Search code examples
linuxbashshellsh

List all directories sorted by size in descending order


I have a requirement to sort all directories of current directory in descended order by size.

I tried following

du -sh * | sort -rg

It is listing all the folders by size but it's just listing by size of folder by values. However it's not sorting correcting. 100 MB Dir should be listed before 200KB.

Any help will be appreciable.


Solution

  • I prefer to just go straight to comparing bytes.

    du -b * | sort -nr
    

    du -b reports bytes.

    sort -n sorts numerically. Obviously, -r reverses.

    My /tmp before I clean it -

    104857600       wbxtra_RESIDENT_07202018_075931.wbt
    815372  wbxtra_RESIDENT_07192018_075744.wbt
    215310  Slack Crashes
    148028  wbxtra_RESIDENT_07182018_162525.wbt
    144496  wbxtra_RESIDENT_07182018_163507.wbt
    141688  wbxtra_RESIDENT_07182018_161957.wbt
    56617   Notification Cache
    20480   ~DFFA6E4895E749B423.TMP
    16384   ~DF543949D7B4DF074A.TMP
    13254   AdobeARM.log
    3614    PhishMeOutlookReporterLoader.log
    3448    msohtmlclip1/01
    3448    msohtmlclip1
    512     ~DF92FFF2C02995D884.TMP
    28      ExchangePerflog_8484fa311d504d0fdcd6c672.dat
    0       WPDNSE
    0       VPMECTMP
    0       VBE