Fish/Bash Script for File and Directory Size

I was looking for a simple catch-all solution to find either the size of a file or the entire size of a folder (and everything in it), in a human-readable format.

Et voilà, here are the scripts in both fish and bash:

fish

# `sizeof`: get the size of a file/directory in human readable format
function sizeof  
    if test -d $argv
        dirsize $argv
    else
        filesize $argv
    end
end

function filesize  
    ls -lah $argv | awk '{print $5}'
end

function dirsize  
    du -sh $argv | awk '{print $1}'
end  

Just add this to your config.fish, or even better, as a function in your fish functions directory at ~/.config/fish/functions/sizeof.fish.

bash

# `sizeof`: get the size of a file/directory in human readable format
function sizeof {  
    if [[ -d $1 ]]; then
        dirsize $1
    else
        filesize $1
    fi
}

function filesize {  
    ls -lah $1 | awk '{print $5}'
}

function dirsize {  
    du -sh $1 | awk '{print $1}'
}

And add this either to your .bashrc or your .bash_functions file.