Some basic command line tips, just for jazz

Here are a few command-line tips, some basic stuff that I use regularly but that novices may not have discovered yet:

open

The open command is cool because it bridges the Terminal and GUI worlds. From its man page: “The open command opens a file (or a directory or URL), just as if you had double-clicked the file’s icon.”

Say you’re in your home folder in Terminal, and you decide you want to open the folder in the Finder. Type open . (and hit return). The Finder comes to front with the current folder open. (You can of course also specify a folder, as in open /usr/share/calendar)

But open also opens other things. For instance, you could launch Chess like this:

open /Applications/Chess.app

Or launch a URL in your default browser:

open 'http://example.org/'

Or open a file:

open path/to/some/file

Aside about tab completion

Typing the above command open /Applications/Chess.app doesn’t look easy, actually. But here’s what I type when I do it:

op[tab] /A[tab]Ch[tab]

Tab completion is your friend. Without it, using Terminal would be unbearable.

pushd/popd: snapback for directories

You know snapback, right, in Safari? How you can snap back to a previous page?

You can do something similar with directories in Terminal. Say you’re some place odd, and you have have to go somewhere else, but then you want to come back easily.

Instead of just doing a cd to someplace else, use pushd, as in pushd /some/other/directory. When you’re finished in some-other-directory—when you want to snap back to where you were—type popd.

(It’s actually a stack, but I’m not going to explain that right now. Plus, most of the time I myself just use it as a quick snap-back feature rather than as a stack.)

What day of the week is the 23rd?

I myself don’t have iCal running all the time. And questions like this come up from time to time—what day of the week is the 23rd? What’s the date of the last Tuesday of this month?

These questions can be easily answered with a quick look at a basic calendar for the current month. Type cal and hit return. Easy.

Quickly view a text file

Say there’s a text file and you just want to view it—you don’t need to edit it or anything.

Type less filename (or less /path/to/file).

Less is a fast and easy-to-use text file viewer. Hit the space bar to scroll down. Type b to scroll backwards. Type q to quit. Type h for help. Type /, followed by a search string and the return key, to search for something.

04 Apr 2005

Archive