Archive for the ‘Mac’ Category

Command Line History

Thursday, April 17th, 2008

Here’s a fun little game that Brandon linked to me from Rail Spikes. See what you’ve been running from the command line recently!

matt@Valhalla:~$ history 1000 | awk '{a[$2]++}END{for(i in a){print a[i] ” ” i}}’ | sort -rn | head
111 ls
71 /Applications/Firefox.app/Contents/MacOS/firefox
67 cd
28 tar
25 rm
25 latex
22 exit
22 dvipdft
20 vi
16 mv

Incidentally I’m running Firefox from the command line in order to switch user profiles (they’re still there from the old Mozilla days, but switching is a bit of a secret now — run firefox -ProfileManager)

Replacing ls in OS X

Saturday, November 10th, 2007

Ever since I replaced my laptop with a MacBook in January of this year I’ve been impressed with Apple’s OS X - in fact I’ve moved over to using it entirely, having replaced my desktop with a Mac Pro in July. It has a very snazzy interface and overall good user experience, and underneath all that prettiness it’s running on BSD so I can hack away via terminal all I like. However, coming from a Linux background, I’ve found the color options for the default BSD ls command lacking. With the GNU ls, you have a very large degree of control over how things look by using the .dir_colors file in your home directory. With that in mind, I decided to replace the default ls using this simple method.

First, install XCode Tools, found on the original OS X disc. Alternatively, you can download it from Apple, though it’s about 1GB. There are a number of useful developer tools included, but what you really want is gcc, the GNU C Compiler. (If you do much programming, you’ve probably already installed this) The idea here is we’re simply going to compile the GNU ls and dircolors for our Mac. If you’ve used Linux much, you’ll recognize the steps exactly.

Next, download the newest version of coreutils from the GNU FTP. Pop open a terminal and make a temporary directory, then decompress the archive - for instance tar -xvjf coreutils-6.9.tar.bz2

Enter the directory created from decompressing coreutils. Now we’ll compile for our system. Simply run ./configure and then make when it’s done. If either configure or make gives you any guff, you probably just need a new version of XCode Tools/gcc.

Presuming that all went well, we’ve now got a new ls binary ready to go. To backup your old one (and its man pages) and replace it with the new, simply run:
sudo mv /bin/ls /bin/ls.bak
sudo cp src/ls /bin/ls
sudo cp src/dircolors /bin/dircolors
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
sudo cp man/dircolors.1 /usr/share/man/man1/dircolors.1

Don’t forget to trash the coreutils directory once you’re done with it.

Now all you need to do is run ls --color=auto to get colored output. I suggest adding the line alias ls='ls -hF --color=auto' to your .bash_profile file. This makes ls color-code output, as well as giving helpful symbols to indicate executable/directory/etc status and displaying file sizes in a human-readable format.

To get the full benefit of the color system, you’ll also want to create a .dir_colors file in your home directory, and have dircolors run when you start a shell. Add a line to your .bash_profile such as eval `dircolors`. You may download my .dir_colors and modify it if you like. With all that done, you can get pretty results like this:

ls with colors!