0

Yes, we all love the Macintosh interface but there are times when it can get in the way. Under the hood of that interface is a powerful Unix based computer that can swiftly perform many tasks that are, slow difficult or downright impossible in that shiny GUI.

So, go to the Utilities menu and fire up Terminal.

Early days

When you're using the Terminal you can get mighty tired of typing those long file and directory paths. Instead click and drag on a file or folder and drop it in the window - you get the path and name typed for you.

By the same token it can be murder to click through twelve levels to get to a folder in the Finder that you are looking at in Terminal, not to mention most of the really neat folders are hidden by default. "open ." at the prompt opens the current folder in the Finder.

Do you find yourself running on a local machine and using SSH to talk to several others? Keep them sorted by changing the Terminal window's title for each. Shift-Command-T opens a dialog to set the title and once you've typed it Command-W closes the dialog.

The one problem we have to make sure we take into consideration is that in Unix a space will often separate parts of a command so they can cause problems in file names. Surround file names (and shell variables containing file names) with quotes like "My File", alternately you can use a '' to escape just the space like so : My File.

Quick bash tips

Just a couple of bash tips to get you started. Before you go too far I also recommend you visit The Linux Documentation Project where you can find some good bash tutorialsl. When ready a thorough read of the man page can also be helpful. You will also find the 'man' command useful. Just type 'man' and the command name at the prompt to see the manual page.

CDPATH is your friend. It won't take long before you learn about the PATH special variable that tells the shell where to look for applications. The CDPATH variable tells the shell where to look for directories when you type "cd name". By default it is set to "." which searches just the current directory. If you set it to something longer you can quickly change to other directories, I personally use ".:~:~/dev:/usr:/" which searches the current directory, my home directory, my development playground off my home, /usr, and the root of the system in that order. I tried including "/usr/local" but found that confused things too much with /usr and /. Just add the line below to .profile in your home directory (if it doesn't exist yet go ahead and add it, you're sure to put other things in there before you are finished.)

CDPATH=".:~:~/dev:/usr:/"

By the way, the .profile file is run every time you fire up a new window in terminal, bu only then. If you add to it or edit it your change is not reflected in the current terminal session so after a change type "~/.profile" at the prompt to run it again.

Speaking of cd - "cd -" takes you to your last directory - great for toggling between directories. If you are about to navigate away from a directory and know you are going to return then "pushd" instead of "cd" will push the current directory onto a stack and change to the specified directory, "popd" will take the top entry off the stack and change to it, "dirs" will list the stack.

Colour can also be your friend. I find it hard to see where my commands are when scrolling back through the terminal. So I set my bash prompt to blue. Add the line below to your .profile

PS1="[033[34m]h:W u$[033[0m] "

While we are fiddling with bash let's also improve its ability to read our mind. Here's a few more things to think about adding to .profile

# this makes bash check for common spelling erros in a path

shopt -s cdspell

# this one writes your commands to the history file every time

# so a command in one window can be used in another

shopt -s histappend; PROMPT_COMMAND='history -a'

What to do

Before we get too far I'm sure you want to view the man pages for the commands I've already mentioned. Don't you want to see them in Preview where they look nice and can be easily printed or saved? Add this function to your .profile

manp()

{

man -t $* | open -f -a /Applications/Preview.app/

}

Now when you type 'pman ls' at the prompt you get the man page opened in Preview.

Perhaps you want to read them in your web browser? Then you need bwana. This allows you to open a man page in your browser by typing "man:command" as a URL. The joy of this one is that it hyperlinks all the other commands mentioned so when you go "man:ls" and it says "SEE ALSO chmod(1)" it gets turned into a hyperlink and a click opens the chmod man page. If you want to know why Unix Wizards spend years attaining their status try just "man:" as a URL and look at the list of commands available to you.

So what are my favourite Unix commands. Start with the obvious; ls, chmod, chown, ps and top. Of course we all know about man and apropos. Add to those which and whoami. Oh and if you aren't up to speed with vi (now improved to vim) give vimtutor a whirl.

Now for some Mac specific commands. Foremost of these, for me, are 'open', 'defaults', 'PlistBuddy', 'plutil', 'scutil' and 'dsconfigad' and 'dscl'. Check out the man pages for these and you go a long way to knowing how to manipulate the Mac from the command line. The hard part with defaults and PlistBuddy is knowing what to set and unset. I often save a copy of a preference file or plist, do the changes in an application or system preference and compare them using diff to discover what is changing. New with Spotlight metadata are mdimport, mdls and mdutil.

Other you might like to check out include ditto, osascript, dirt, hdiutil, say, and script.

Further Study

If you've had a good look at the tutorials I've mentioned and read the tutorials on bash and bash programming then you are well on the way to understanding the power of Terminal. If you would like a large list of preferences that can be altered or read using 'defaults' then I recommend http://secrets.blacktree.com/. For other useful commands try a look at Clix from Rixstep which offers a GUI to the command line and a large database of example commands to perform a number of tasks.

Post a Comment

 
Top