Displaying sorted list(size wise) of Files/Folder on Linux

Even though i have a 120 GB HDD on my laptop and have dedicated a significant portion of hard disk space to Ubuntu still somehow every three or four months i run out of disk space . Also i am particularly bad at organizing stuff and well files are cluttered over my entire file system this makes tracking large files i don't need very difficult . Lots of application create a number of temporary files and these files also take significant amount of disk space and could be hard to track down if they are not created in /tmp .


The command I am discussing below helps you solve precisely this problem by giving you an ordered list (ordered in terms of size) of files and folders taking up space in your computer thus helping you find files/folder which are taking up significant amount of space :

du -sm *


Above command would display all the files and folder as well as their size as a list, however this is not a ordered list . To order the list we would need to pipe (that is send output of this command to another ) the output of the "du" command to "sort" command giving out list of files and folders sorted in ascending order according to the size.
du -sm * | sort -nr

Still there is a problem this is an entire list of files and folder in your file system , so if you have number of files and folders it will take number of screens to display . A better way would be to pipe the output of above command to "head" command to display only specified number of lines of output generated by "du -sm * | sort -nr " command .

du -sm *| sort -nr | head -15
Now the above command would display the top 15 files and folder according to their size .


Article Written by : Ambuj Varshney (blogambuj@gmail.com)
For Desktop on Linux Blog , http://linuxondesktop.blogspot.com
(C) 2008 , Ambuj Varshney

Comments

  1. It's probably worth mentioning that the output is in MB, not bytes.

    Initially my first thought on reading this was why not use "-h" and get it in, M, K, G depending, but when I tried it I realised that then the largest number, irrespective of unit, comes first so you could have: 688M, 500K, 4G in that order. Obviously when the list is long then you will miss many of the largest files...

    Thanks for the tip.

    ReplyDelete
  2. Anonymous4:33 PM

    A better solution, in my opinion, would be to use the Disk Usage Analyser application in Ubuntu to drill down graphically. The only downside is that it takes a lot of time to start up.

    ReplyDelete
  3. Anonymous5:38 PM

    I'm not sure about the way you have this command setup. From man du you can see that the -s flag just spits out a summary of everything without listing the files. If you just take out -s, this command works nicely.

    ReplyDelete

Post a Comment

Amazon Ads