Improved ‘ls’ functionality in your terminal

Mav Tipi
2 min readDec 17, 2020

A couple of weeks ago I wrote a post about using your bash_profile to create custom git commands to improve your workflow. Here are some more aliases I’ve found useful.

These are written for a Mac environment, but the same functionality should be possible in Windows and Linux.

Improved ls

ls is the command that lists all files at the current location. Sometimes it can be useful to display extra information, or sort the list. For example, if you want to display the size of each item, you can use

du -sh *

du stands for “disk usage”; it’s already an improved ls, in that it’s ls with extra information, but it needs to be massaged a bit to be useful. In its base state, it traverses every directory and reports each item in the directory; -s * groups directories. -h converts the sizes to bytes (without that, it is in units of 512 bytes).

We can also sort them by size:

du -sh * | sort -h

And we can add a total to the end with -c:

du -sch * | sort -h

Certainly could be useful. So if we want to add this to our bash_profile, we could write it as:

alias lsplus='du -sch * | sort -h'

cd; ls

cd is the command for changing directories; ls is, as we said before, the command for listing. Often you want to do both.

Recall from the last post that when we want to take arguments from the user (in this case the directory we’re cding into), we use function syntax rather than alias.

function cdls() {
DIR="$*"
builtin cd "${DIR}" && \
ls
}

Why don’t we combine this with our improved ls?

function cdls() {
DIR="$*"
builtin cd "${DIR}" && \
du -sch * | sort -h
}

Let me know your favorite bash aliases below.

--

--