OSX Terminal Fun III: A better LS
Mac OS 10.2 for an unknown reason ships with a version of ls which doesn't support colored output. This isn't a big deal, but having colored output for ls can make it a lot easier to quickly tell different types of files from each other, or directories from files (click the picture to the right to see mine).
Yeah, I know you've seen colored output in your Terminal.app before, in man pages and the like, but that is just basic bolding of text (which you can set a preference for under the Terminal.app's prefs).
To get actual colored output from the ls command, you have to replace the standard ls Apple ships with OSX with a more "robust" version which will return colored output to the shell when you use the ls command.
You'll need to make sure you have the Developer Tools installed from the CD that came with OSX (there should have been 3 CD's) and you're good to go. Remember, follow these instructions exactly and I'm not responsble for typos (or anything else, for that matter).
Open up your terminal, and type:
% mkdir newLS
then
% cd newLS/
Cool beans, you've created your working directory so that you aren't making a mess and have changed your path so that you're inside of it.
Now, using your browser right-click (or control-click) on this link and select "copy this url" or "copy this address".
Now in your terminal window again, type:
% curl -O "(paste address here)"
That is -O as in "ohboy", not a zero. Paste the url (using paste from the Terminal's menu, or the apple key plus the v key) you copied from your browser in between the quotes (yes, you need the quotes). Hit return.
Curl will go out, get version 4.1 of the gnu file utilities and write it out to a file within the current directory. You'll be treated to some nice download stats... I prefer curl, but wget works fine too if you know it.
Type these commands in order. Remember, a % symbol means you should be doing it on a new line and seeing a prompt.
% tar -xzf fileutils-4.1.tar.gz
% cd fileutils-4.1/
% ./configure
% make
./configure and make might take a few minutes depending on the speed of your computer and disk, but afterwards you'll have an updated, freshly compiled ls waiting for you. The below commands will backup your old ls, rename it to ls.bak and move your fresh ls to where it needs to be.
% sudo mv /bin/ls /bin/ls.bak
% sudo cp src/ls /bin/ls
% rehash
The "% rehash" command refreshes your shell environment, but you might want to create a new terminal window anyways. Do not close the current terminal window though!
Now, when you type the following command you'll see a nice color-coded display of your files:
% ls -ohS --color=auto
Since you've replaced the ls that comes with OSX, you're going to want to replace it's man page as the options (such as color) will be different.
% sudo mv /usr/share/man/man1/ls.1 /usr/share/man/man1/ls.1.bak
% sudo cp man/ls.1 /usr/share/man/man1/ls.1
Now, clean up your mess. ls was just one utility you compiled, so you might as well get rid of the rest. Use these commands to clean everything up.
% cd ..
% cd ..
% sudo rm -rf newLS/
All the other utilities are now deleted. Obviously you aren't going to want to type something like that out whenever you type want to use the ls command, which brings us to aliases...
Comments (7)
Posted by: drunkenbatman at April 29, 2003 04:16 PM
yw. the pic i put up was a bit old, and i changed the dir colors from stock as you're right, they can be a little rough to read.
you have to edit your rc file to export the colors you want as environment variables (in ascii format, not cleartext), i have mine set to a lightish purple. i'll probably put that up next.
Posted by: pam at July 18, 2003 12:13 AM
configure: error: no acceptable cc found in $PATH
I tried to do the install on a very fresh version of OS 10.2.6
Have you seen this problem before?
Posted by: drunkenbatman at July 19, 2003 03:01 PM
Did you install the developer tools that come on a seperate CD? It can't find the compiler (which isn't installed on a normal client machine, but comes with the developer tools on the CD that came with OSX).
Posted by: Pablo at May 6, 2004 02:46 PM
Hey, i like the idea of coloured term. It builds very smoothly - as long as your MAC's time is up to date.
Thanks a mil,
Paul
Posted by: Giulio at March 3, 2006 06:22 PM
well.... it seems that there is a more ortodox way to get colors in ls:
ls -G
pretty amazing hu?
Posted by: random at March 29, 2006 12:07 AM
AFAICT, the BSD 'ls -G' is a less functional feature than GNU 'ls --color=auto'. The former is customizable based on file attributes; the latter allows the same, but also allows for color customization based on patterns in the file names. For instance, if you have LS_COLORS=':*.gz=01;31', then GNU ls will display gzip files in bright red.
Is there a way to do this with the stock OS X ls?








THANK YOU THANK YOU THANK YOU for giving me back colored output! That always bothered me about OSX! Why does it not have support built in?
It was kool to see curl too. I have always used wget. Why did you use curl instead?
Oh and like how though do you change the colors for directories and files? The files are all ok but the default blue for directories with my background black instead of the default white makes them very hard to see. I would like to change the color.