bashsortingdualiases

Bash aliases du - sh + sort


Here is my aim: I want to see disk usage AND have the list sorted by size AND make it an alias.

I read on the forum that du -hs somedirectory |sort -h would do the first two jobs, which is true. How can I put this in my .bash_aliases ?

The next line works, but I cannot choose the directory anymore

alias du="du -hs * |sort -h"

Thank you for your help !

R


Solution

  • You can't pass dynamic arguments to the first command in alias. You need to use function to achieve this:

    dirsize() {
        du -hs $* | sort -h
    }
    

    Put this in your .bashrc file. Then,

    $ dirsize Development/ Documents/ Videos/
    86M Documents/
    92M Development/
    5.6G Videos/
    

    To make an alias:

    $ alias du=dirsize