Install better ls, du, and df tools in 10.3

Apr 12, '04 10:33:00AM

Contributed by: jhegarty

The standard ls that comes with 10.3 doesn't give us any colors. du and df report absurd numbers. du also doesn't take the "--max-depth" argument. These things can be annoying, so here's a way to fix them. ;)

** note: this hint assumes you have gcc or another working compiler installed, and that you're using /bin/bash as your shell. otherwise, it's up to you to interpret the instructions to your shell's scripting syntax. **

[robg adds: This hint is an update to several older ones (including two on adding color to the ls in Panther) which discussed similar solutions...]

First Step: Get New Software

For all of these features, you need GNU Fileutils, or GNU Coreutils. You can get these from ftp.gnu.org. Be sure to also visit the GNU webpages regarding Coreutils and Fileutils. coreutils is a later version than fileutils, and includes the shellutils and textutils sets as well. coreutils will also install via Darwinports, so if you have that, all you have to do is

port install coreutils
It will put all the utilities into /usr/local/bin (or other location you've specified to Darwinports), with all the commands prefixed with a g, like gls and gdf. If you install with Darwinports, you can go straight to Step Two as soon as it's done installing.

Otherwise, if you don't use Darwinports, you can download and compile from source. I've tested compiling fileutils v.4.0 manually on 10.3 with gcc 3.3 (the one that came with Panther's Dev Tools), and it worked just fine. I installed the latest version of coreutils with Darwinports, which does in fact compile the package from source, and since that worked, it's a safe assumption that you can probably download any version and do the standard
./configure --prefix=/path/to/install/to
make
make install
Note: Be sure to read the INSTALL and README that come with the source package, as different versions may or may not need configure flags for OS X, for specifying /usr/local/bin vs. /bin, or for adding the "g" prefix to the filenames. Your standard ls and other commands live in /bin, so be sure to back them up if you decide to completely replace them. I haven't tried outright replacing the commands, so I have no idea what it might break, but I don't recommend it. If you do replace them, and nothing breaks, you can skip to customizing the colors (and leave a comment to tell us whether it works if you do that!). You'll need admin or root priv's to install anywhere outside of your home directory, so if you don't have those permissions, you can still get the benefits of GNU utilities by installing to a "bin" directory in your home area, and adding that to your PATH during Step Two

Second Step: Set your shell to use the new software!

At this point, you should have the GNU coreutils or fileutils installed, and you should know where they are installed. If you installed with Darwinports or configured from source and didn't specify a path, they're probably in /usr/local/bin, prefixed with a "g".

An easy way to tell your system that you want to use the new ls or "gls" (with color) instead of your old one is to make an alias in the your login script. The bash shell under Terminal sources the file ~/.profile, or /etc/profile (if you're using another shell, it's up to you to know which files it runs at login). If you have admin privledges, you can edit /etc/profile, and the updated commands will be automatically used by everyone. If you don't have these privledges, edit or create a ".profile" file in your home directory.
Here's what you'll need to add (at least):
alias ls='gls --color=tty'
alias du='gdu'
alias df='gdf'
You can make aliases for as many of the GNU programs as you'd like.
Note: In order for these to work, you'll need the location of these new programs in your PATH. Type
echo $PATH
in your Terminal to find out. If the location isn't listed in the output, add the following to your .profile or /etc/profile, above the "alias" lines:
export PATH=$PATH:/path/to/install/dir
(where "/path/to/install/dir" is the location you installed the programs)

Before continuing, save the /etc/profile or .profile, then open a new terminal and make sure you're getting the aliases rather than the old programs. Use
type ls
to find out. If it says "ls is aliased to `gls --color=tty'", then you're good to go! Otherwise, you'll need to figure out why your Terminal isn't sourcing the file you thought it was. The first place to look is Terminal:Preferences, but a full investigation is beyond the scope of this hint.

Customizing the directory Colors

You may have noticed that you only get a couple of the standard terminal colors, even with "gls --color=tty", and you'll quickly find out why if you do:
echo $LS_COLORS
It's blank!!! What do we do? Well, GNU fileutils came with "dircolors" or "gdircolors", but even if you run it, it doesn't tell you much. The secret is to add the following color codes into your .profile or /etc/profile:

export LS_COLORS='no=00:fi=00:di=01;34:ln=01;36:pi=40;33:so
=01;35:bd=40;33;01:cd=40;33;01:or=01;05;37;41:mi=01;05;37;
41:ex=01;32:*.cmd=01;32:*.exe=01;32:*.com=01;32:*.btm=01;
32:*.bat=01;32:*.sh=01;32:*.csh=01;32:*.tar=01;31:*.tgz=01;31
:*.arj=01;31:*.taz=01;31:*.lzh=01;31:*.zip=01;31:*.z=01;31:*.Z
=01;31:*.gz=01;31:*.bz2=01;31:*.bz=01;31:*.tz=01;31:*.rpm=0
1;31:*.cpio=01;31:*.jpg=01;35:*.gif=01;35:*.bmp=01;35:*.xbm=
01;35:*.xpm=01;35:*.png=01;35:*.tif=01;35:'
Note: If you copy and paste that, make sure you remove any line breaks, or otherwise adjust for them, else your shell will give you errors when you login.

That's it!!! Have fun!!!

Comments (24)


Mac OS X Hints
http://hints.macworld.com/article.php?story=20040410043824914