Thursday, 09 August 2007

a handful of unix (Part 1) (for new mac users): ls, locate, man, export $PATH

Here are some tips which I've needed to use recently in setting up my new mac (yes, I upgraded)... You'll need to use your handy terminal, though I prefer using iTerm. I've also started with very basic commands for absolute newby's:


In terminal, the UNIX commands on a mac are similar to your old DOS commands. With a few noteable exceptions. Go ahead, open up a terminal (Applications -> Utilities -> Terminal) and type the following: ls

ls is short for list, it's similar to the DOS command dir. Now try ls -al
See, more details, including hidden files, which are prepended with a period.

cd changes directory. cat displays the contents of a file. less does the same but allows you to scroll.


Very simple. Type man locate to get the manual pages for the locate command. It's a text help file explaining how to use the commands. If you want to see how to properly use the man pages, you can see the man page for man... so that would be typing man man.


This function is the terminal equivalent of Spotlight, except, it looks everywhere! You simply type in locate php.ini, and it finds all the file references of php.ini for you. At first run I got an error message saying the database wasn't present. A brief look in the man page and I found the command to update the database (you should ideally run this every so often to just update all your references for locate. Do this before you run locate if you haven't used locate for a long while) - /usr/libexec/locate.updatedb

(It might be different on your system, so then just read your man page to find out how to update the locate-DB)

export $PATH

OK, this is more of a self note for me, so that I can use it if i need to do this again, because some of the bookmarks I had for $PATH stuff no longer link to valid sites... phhffft. ok.

For a shell script, the PATH variable used is stored in the .profile file (technically if you're using BASH - if you have no idea what i mean, ignore this) in your user directory. You can set up Textmate to respond and edit files by using the mate command from a shell. (here's how)
So, once this is done, open up .profile in Textmate and edit it. Mine looked like this:

test -r /sw/bin/ && . /sw/bin/
export PATH="$PATH:/usr/local/bin:/Applications/xampp/xamppfiles/bin"
export EDITOR='vi'

I edited this and changed it to:

test -r /sw/bin/ && . /sw/bin/
export PATH="/Applications/xampp/xamppfiles/bin:$PATH:/usr/local/bin"
export EDITOR='mate -w' #textmate as editor, waiting for file to close before continuing
export LESSEDIT='mate -l %lm %f' #textmate as editor for the LESS command, by pressing v

What export does, is make the PATH variable available to sub shells, and you put it in .profile because that's read when you fire up a shell. A shell is a session in Terminal or iTerm (in simple terms).

Something to remember: UNIX commands in the terminal are case sensitive, which means the command locate will work, but Locate or LoCatE will not work.

OK, most of you are either bored to tears cos you know already, or bored to tears cos you have no idea or inclination to get into the shell. This is for the other folks, yes, both of them!

No comments: