Howtos / Articles

List Folder and File Size and Sort By Size on Linux

A common requirement on any operating system is to be able to view the total size of a list of folders and files on the file system, and be able to sort the list by size. Generally this isn’t straight forward to do. The commands below can be used to do just that.

Display folder sizes and sort in ascending order:

$ du -sBM * | sort -n

Example:

$ du -sBM * | sort -n
0M      man
1M      etc
1M      games
1M      include
1M      sbin
1M      src
3M      lib
4M      bin
27M     share
$

Display folder sizes and sort in descending order:

$ du -sBM * | sort -nr

Example:

$ du -sBM * | sort -nr
27M     share
4M      bin
3M      lib
1M      src
1M      sbin
1M      include
1M      games
1M      etc
0M      man
$

List Sizes with an overall total:

$ du -csBM * | sort -n

Example:

$ du -csBM * | sort -n
0M      man
1M      etc
1M      games
1M      include
1M      sbin
1M      src
3M      lib
4M      bin
27M     share
33M     total
$

Create an alias:

If you often need to check folder and file sizes, rather than memorize the above commands, you can set up an alias, and put it in your .bashrc file so its always available.

$ alias du-sort="du -csBM * | sort -n"

Then you can just issue the command using:

$ du-sort

Example:

$ du-sort
0M      man
1M      etc
1M      games
1M      include
1M      sbin
1M      src
3M      lib
4M      bin
27M     share
33M     total
$

Add this alias to the .bashrc file in your home folder. This may change depending on your distribution.

$ nano ~/.bashrc

Simply add the alias line to the end of this file, save it, log out, and log back in, and you should still be able to use the ‘du-sort’ command.



Share this howto article!
  • Facebook
  • Twitter
  • Reddit
  • email

Leave a Reply

  

  

  

You can use these HTML tags

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>