Pimp My Shell! Making ‘ls’ more useful with less typing!

The ‘ls’ command is arguably one of the most commonly used commands that you will type on a regular basis when working from a terminal. So it only makes sense to spend a few minutes optimizing it’s usefulness!

I’m going to assume you’re using a BASH shell, because well… that’s what I use and know how to work with. But it’s also the most widely used shell, so chances are this will work for you (and it may work for some other shells as well, so let me know your mileage with this in a comment).

Okay, so fire up your favorite editor and open the ‘.bashrc’ file from your home directory.

nano ~/.bashrc

First, make sure you don’t have some config lines (like ‘alias’ entries or other stuff) dealing with ‘ls’ already in place in this file. If you do, comment them out (e.g. slap a # in front of those lines).

Paste this in to your ‘.bashrc’ file:

# Pimp my 'ls'
export LS_OPTIONS='--human-readable --size --classify --group-directories-first --color=auto'
eval "`dircolors`"
alias ls='ls $LS_OPTIONS'

Okay, lets go over what each of these lines do!
1) export LS_OPTIONS=’…’: This line sends a slew of option switches to the ‘ls’ command by default whenever you execute the command. Let’s go over each of them:
    a. ‘–human-readable –size’: this shows the file size next to each item returned by the ‘ls’ command, in a human readable format (i.e. 4.0K instead of 4096).
    b. ‘–classify’: this does a few things, but I’m using here primarily so we can get a forward-slash (/) after directory items in our ‘ls’ listings. It just makes it that much more easy to see which items are folders and which are files. Especially helpful if your system doesn’t support colors!
    c. ‘–group-directories-first’: This one is obvious… right?
    d. ‘–color=auto’: enables colorized output. I.e. directories are a different color than files, and sym-links, etc. This is very nice for getting the gist of things ‘at a glance’.

2) eval “`dircolors`”: Honestly, I’m not sure why this is necessary. Obviously it has something to do with the –color switch, but it seems to work for me with or without this line. Debian recommends it, so I’m leaving it to be safe. Sorry…

3) alias ls=’…’: This line sets up the alias for the ‘ls’ command. If you’re not familiar with what an alias is, basically it’s what allows us to set up all these default options and have it work whenever we issue an ‘ls’ command. It’s neat because we can still pass other command switches to ‘ls’, like ‘ls -lA’, and it will work as expected. Neat, huh?

Well, that’s about it! Enjoy!


Leave a Reply