OS X Terminal for Fun and Profit


Sitting underneath the shiny, if somewhat grey UI in OS X Lion is the profoundly useful Terminal.app, which lets you fiddle about in the Unix-based underpinnings of the OS. While using this is almost never necessary, it is often profoundly useful. Most of the time, this is mostly useful (for me) for easy access to Unix clusters for scientific computing, terminal output for programming in Python or C, etc.

But it’s also stupidly useful for customizing your set-up in ways not necessarily supported in the UI, without the use of a third-party utilities, exactly the way you like it. Two of the most useful things I’ve found recently are described after the break.

First, a warning – you are fiddling about with the roots of your OS. Please at least backup your system before you try this. Also, while these have all worked for me, if you somehow blow up your machine, it’s not my fault.

Two other notes: These were all run on an administrator user account. $ is used to denote the end of the prompt in your terminal shell. Don’t type that bit.

Trick 1: Automatic Backup to DropBox

DropBox is stupid handy for keeping your files “in the cloud” and having access to them anywhere. But what if you want to be able to  be able to backup and use files outside your main Dropbox folder? One line of Terminal code and you’re done. Exploiting “Symbolic Links”, which essentially tell your machine to treat one directory like its in another place, you can link files outside your DropBox folder to one inside it, like so:

$ln -s /Volumes/HardDriveName/username/Documents/ImportantFiles ~/Dropbox/

That takes the folder “Important Files” under your Documents folder and places it in the main directory of your Dropbox. Edit the paths as necessary or, once you’ve typed “ln -s ” simply drag the folder you want to make the link from to the terminal window – the system will automatically fill in the appropriate path. And there you go – instant backup of any directory you want onto your DropBox. Which gives you 2 GB of space for free.

Trick 2: Color Changing Terminal Windows when using SSH

This one is a little more advanced, but in my mind, probably more useful. I often have several Terminal windows up, some of which are running tasks on my own machine, some of which are connected to one of several servers. This makes it occasionally difficult to keep track of what exactly I’m doing. While I can manually use a different Terminal theme for each task with my own personal coloring scheme – but its nicer if the system will do it for me.

Step 1. At your main user directory (the one you can get to by typing cd ~), check and see if you have a file called .bashrc . If you don’t, make a blank document using a text editor like TextWrangler or BBEdit.

Step 2. Open your .bash_profile file in the same directory using a text editor and type the following:

if [ -f ~/.bashrc ]; then         
     . ~/.bashrc 

This lets functions put into your .bashrc file be used.

Step 3. In the Terminal application, make a color scheme you’d like to use when SSHing – this can be a general scheme, or one for each server. In this example, lets assume I made one for SSH tasks generally. Name it something useful, like “SSH-Theme”.

Step 4. In your .bashrc file using a text editor, put in the following:

function tabc() {
NAME=$1; if [ -z "$NAME" ]; then NAME="Pro"; fi
osascript -e "tell application \"Terminal\" to set current settings of front window to settings set \"$NAME\""
alias sshiron="tabc SSH-Theme; ssh -X confounding@bigiron.university.edu; tabc"

What’s this do? Generally speaking, it means the Terminal uses the “Pro” theme. But, if I type $ sshiron , the Terminal changes to the new SSH-Theme and logs into the SSH server – in this case, a pretend server at bigiron.university.edu. Edit things accordingly for your own setup. When you exit, your window will return to the Pro theme.

2 Responses to “OS X Terminal for Fun and Profit”

  1. 1 The Confounded by Confounding Archive | The Academic Mac
  2. 2 My Top 5 Mobile Apps for Scientists « Confounded by Confounding

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: