Miscellaneous Terminal Basics

This is a random list of useful stuff for Terminal beginners. I’ll just add things as they come to mind.

TextWrangler

Rather than trying to faff around in Pico or vi, you can use the excellent and free Textwrangler from Barebones.

Want to edit an invisible file like .profile? Just drag a directory onto TextWrangler in the dock, and you can edit it from there. Alternatively, just type


$ edit foo.txt

and foo.txt will open.

The .profile file

In your home directory, there is an invisible file called .profile . You can edit this file to add shortcuts for common commands and various other customizations. So open up a new terminal window:


$ cd
$ cp .profile .profile.saved
$ edit .profile

We can now edit the file in TextWrangler, and we’ve also created a backup (which we can restore with: mv .profile .profile.broken && mv .profile.saved .profile)

Add these 2 lines to the .profile file and save it:

alias h='history 10'
alias reload="source /Users/YOU/.profile"

[ replace /Users/YOU with the path to your home directory . If you don’t know this, then in terminal type cd followed by pwd and it’ll appear]

Before you can use these changes you have to reload the .profile file by typing:

$ source ~/.profile
$ h

Do that now. But notice that we added that line as an alias, so from now we can just type reload instead.

Now type

$ h

You’ll get the history of the last 10 things you did numbered. You can repeat a previous action by just doing !527 (or whatever the appropriate number is). !! repeats the last command.

Command Path

When you run a command, how does the Terminal (more properly the shell) know where to look to execute it. Basically, there is a variable called PATH which tells the shell where to look? Try

$ which pico

It should output

/usr/bin/pico

which tells the computer to execute pico which is inside /usr/bin

But why /usr/bin? Type

$ echo $PATH

and you should see something like

/usr/bin:/usr/local/bin

which tell the terminal where to look. (Never put stuff in /usr/bin by the way. That’s for the system, whereas /usr/local/bin is stuff that can added by admins for users to use.)

What happens if there is a pico inside /usr/bin and /usr/local/bin? Well, /usr/bin appears before /usr/local/bin so it will execute /usr/bin/pico. If you want to execute /usr/local/bin/pico then you’ll need to type out the whole path.

Now it’s often handy to be able to add executables to your own bin directory, and not have to type out the full path: /Users/YOU/bin/foobarit, but just foobarit instead. To do that add a directory in your home called bin, and add or modify your .profile file so it has a line like this:


PATH=$PATH:.:/Users/YOU/bin

Now when you type in a command, the shell will look in this order:


/usr/bin /usr/local/bin .(the present directory) /Users/YOU/bin

You want your bin to be last to prevent some nasty person dropping an cd (or something) file into your own bin modified to send private information about you. (Imagine typing cd and rather than executing /usr/bin/cd instead a modified ~/bin/cd got executed instead)

Digg!

0 Responses to “Miscellaneous Terminal Basics”


  1. No Comments

Leave a Reply