This is a rant I have made many times in person: far too many to count. Since I can't possibly warn everybody who might be affected by this, face to face, I'm hoping you will all spread the word.
To begin with, I should say that
ls --color is useful. It can show you which items in your directory listing are directories, symlinks, broken symlinks, device nodes, named pipes, whatever. I am glad that it exists.
However, let’s consider the basic action of ls. It is, of course, to display which files and directories exist in a directory. This is really quick and easy in Unix. Simply use the libc function
opendir() then just
readdir() till it returns
errno is 0. In terms of syscalls, this is an
open(), one or two
read()s and a
When you turn on the
color option, every directory item incurs the weight of an extra
stat() call to see what it is.
Worse still, if it’s a symlink,
ls will obligingly bound across to the thing linked to, to check it exists. If this causes your automounter to wake up and mount something remote, that’s quite an overhead. If your mount hangs because the remote filesystem is unavailable, you can say goodbye to that process; it'll be stuck blocking on a syscall that will never return.
A partial solution
To fix the symlink problem, here’s what you do.
ls was written to avoid chasing symlinks if configured not to. Check your environment variables for the LS_COLORS variable with:
$ env | grep ^LS_COLORS
If it exists, you have a config file for colour ls: you should edit
/etc/DIR_COLORS* or copy it to
$HOME/.dircolors and edit it there. Otherwise, type:
$ dircolors --print-database > $HOME/.dircolors
The edit you need to do is to remove or comment out the ORPHAN line from the file.
Unless you were able to edit the file in
/etc, you now need to make sure that this new
$HOME/.dircolors file is used. In your shell login script (
.bash_login for bash and
.login for (t)csh, put the line:
eval `dircolors $HOME/.dircolors`
(Note that these are backticks, not apostrophes.)
Next time you log in, you'll be able to use colourful ls without fear of a remote filesystem freezing your directory listings.
ls --color when you need it, but as with all powerful tools, use it with care.
Corrections: James pointed out that to dump the default config it is –print-database not –print-directory.