Linux Fu: Build a Better Ls [Hackaday]

View Article on Hackaday

Ask someone to name all the things they can find in a room. Only a few will mention air. Ask a Linux command line user about programs they use and they may well forget to mention ls. Like air, it is seemingly invisible since it is so everpresent. But is it the best it can be? Sure, you can use environment variables and aliases to make it work a little nicer, but, in fact, it is much the same ls we have used for decades. But there have always been moves to make better ls programs. One of them, exa, was recently deprecated in favor of one of its forks, eza.

One thing we liked about eza is that it is a single file. No strange installation. No multiple files to coordinate. Put it on your path, and you are done. So installation is easy, but why should you install it?

Features

One of the nice features of eza is that it has better defaults. Sure, you can work around that with ls, but it is still nice. It also knows about things like hyperlinks, Git repos, and even icons — sort of.

By default, the program ignores . and .., even if you give the -a option. You can, however, use -a twice to show those directories, as well. Want to know if a file is tracked or ignored in Git? Use the --git option. If you want to see file permissions in octal (and who doesn’t?), try -o. The --git-ignore option allows you to skip things you’ve already told Git not to worry about. You can even get a nice tree-like output (-T).

Column headers are a simple but nice feature (-lh).

A few output lines from eza

Of Course…

Of course, ls has a lot of options, too. You can suppress the . and .. directories, for example, with -A. You can also ask it to create hyperlinks. In fact, if there is one complaint we have, it is that the options are different, and many scripts use ls. So replacing ls with eza can be a problem. You might be better off aliasing it only for commands you enter. It would be nice if, for example, eza took normal ls options until you used a special option to trigger extended options.

If you want, for example, to list only directories using stock ls, you usually use one of two options:

ls -d */ls -l | grep ^d

With eza, you simply say:

eza -D

Then Again…

Eza isn’t going to change your world. In general, you can do everything it does with some combination of existing tools or ls options. But it is handy, especially if you work with tools it knows about like Git. Another common choice is LSDeluxe, known as lsd. This is widely supported and also has a configuration file, so you can make its defaults what you want.

A few output lines from lsd

The lsd program is probably a bit more compatible with ls, which we like. It also offers tree view, icons, and Git modes. One thing we definitely like is the ability to specify –classic to get output similar to what you get from the stock ls.

Overall, they are both pretty similar. Which one you choose will probably fall to your personal preference. Or, maybe you want to stick with the old Unix principle that programs should do one thing and only one thing well. Then you might stick with the old way of doing things, and that’s fine too. Linux is, after all, about choice.

Grep is another one of those tools you might consider replacing. Of course, if you really want a different experience, try changing shells.