If you find yourself in the Mac OS X Terminal a lot, here are some commands and tools you may find useful.
Consider this part a command-line primer. Three things are useful to remember about the Terminal:
In Mac OS X, you can get to the Terminal by going to Finder > Applications > Utilities and then double-clicking the Terminal application.
I prefer typing to clicking whenever possible so I have installed an application launcher called Quicksilver. It allows me to fire-up applications without using the mouse or trackpad. Very handy!
I’ll probably use the words “directory” and “folder” interchangeably. They mean the same thing. If you are used to a graphical user interface (GUI), the term “folder” will likely be familiar to you.
ls is short-hand for “list”.
ls lists the contents of the folder you are currently in.
$ ls Desktop Downloads Library Music Public Documents Dropbox Movies Pictures
ls can give very detailed information about the files it displays. To do that, you can give it an option or argument.
$ ls -l
- says options come after this. Each letter after the
- is an option. In the above case,
l means long. So
ls will display a long list with detailed information about the contents of the current folder.
total 0 drwxr-xr-x 2 sausande staff 68 27 Aug 12:11 Desktop drwxr-xr-x 2 sausande staff 68 27 Aug 12:11 Documents drwxr-xr-x 2 sausande staff 68 27 Aug 12:11 Downloads drwxr-xr-x 2 sausande staff 68 27 Aug 12:11 Dropbox drwxr-xr-x 2 sausande staff 68 27 Aug 12:11 Library drwxr-xr-x 2 sausande staff 68 27 Aug 12:11 Movies drwxr-xr-x 2 sausande staff 68 27 Aug 12:11 Music drwxr-xr-x 2 sausande staff 68 27 Aug 12:11 Pictures drwxr-xr-x 2 sausande staff 68 27 Aug 12:11 Public
Let’s try another set of options:
$ ls -al total 0 drwxr-xr-x 11 sausande staff 374 27 Aug 12:11 . drwxr-xr-x 3 sausande staff 102 27 Aug 12:11 .. drwxr-xr-x 2 sausande staff 68 27 Aug 12:11 Desktop drwxr-xr-x 2 sausande staff 68 27 Aug 12:11 Documents drwxr-xr-x 2 sausande staff 68 27 Aug 12:11 Downloads drwxr-xr-x 2 sausande staff 68 27 Aug 12:11 Dropbox drwxr-xr-x 2 sausande staff 68 27 Aug 12:11 Library drwxr-xr-x 2 sausande staff 68 27 Aug 12:11 Movies drwxr-xr-x 2 sausande staff 68 27 Aug 12:11 Music drwxr-xr-x 2 sausande staff 68 27 Aug 12:11 Pictures drwxr-xr-x 2 sausande staff 68 27 Aug 12:11 Public
Notice anything different?
It’s the same list but now there is a directory called “.” and another called “..”. Any file or folder in UNIX that starts with “.” is a hidden folder. The option
a used with
ls allows us to see hidden folders.
Dots have special meaning in the command line. They are not punctuation.
. means the folder I am in right now while
.. means the parent folder of the folder I am in right now.
~ is your home (or more accurately your home folder). It’s the place where all the files and folders under your username are kept.
If you’ve ever seen a t-shirt with the words “There’s now place like ~”, now you can laugh because you finally get the pun.
mkdir is used to make folders (or “directories”).
$ mkdir Projects $ mkdir Homework
If your folder name needs spaces, you can enclose it in
"s, just like this:
$ mkdir "Project Proposals" $ mkdir "Stuff I need to do like yesterday"
pwd stands for “print working directory”. It let’s you know where you are in the system directory hierarchy.
$ pwd /Users/sausande
touch will create an empty file with the name you give it. For example:
$ touch the_milky_way.txt
If the file exists, the timestamp on the file with be updated (which ends up being quite a useful thing to do).
TAB key during a command will suggest possible ways to autocomplete the command. You may need to install bash_completion for fancier functions. Out of the box, though, tab completion is useful to drill down folder hierarchies.
cd stands for “change directory”. If you need to move from one directory to another, use
cd with the name of the folder you’d like to move to. We can also use the dots to move around folders.
Let’s say we had the following hierarchy of folders
$ mkdir -p stand/and/sing/of/zambia/proud/and
Did you see what we did there? We created folders within folders with a single command! Thanks to the
p option supplied with
cd we can move down the hierarchy one step at a time.
$ cd stand $ ls and $ pwd /Users/sausande/stand $ cd and $ ls sing $ pwd /Users/sausande/stand/and
To move up the hierarchy we can use
$ cd .. $ ls and $ pwd /Users/sausande/stand
cd to your home folder using either of the following commands:
$ cd $ cd ~
TAB completion, you can drill down all the way to the lowest level in the folder structure.
$ cd stand[TAB]/and[TAB]/sing[TAB]/of[TAB]/zambia[TAB]/proud[TAB]/and
It takes some getting used to. Soon you’ll find yourself hitting
exit will exit your Terminal session. If you explicitly say so in your preferences,
exit will also close your Terminal window after exiting.
open will open a file or a folder for you.
If you are in a folder you can use
open . and the folder will open as a Finder window. If you have a file called
index.html in your current folder,
open index.html will open the page in your default browser.
bc is a command-line calculator. It’s a bit trippy but really useful.
say speaks text.
I use it to let me know that a long-running task has completed.
say "FTP website upload completed." say "Hello, shall I prepare you some chikanda?"
I enjoy that it does not wince at trying to pronounce words that are not English.
date displays the current system time. The best part of it is the number of ways it allows you to format a date.
$ date Wed 29 Aug 2012 12:54:32 CAT $ date "+Today is a %A" Today is a Wednesday date "+%B/%d/%[email protected]%H:%M:%S" August/29/[email protected]:57:25
tail displays the last 10 or so lines of a file. When used with the
f option, it will stream the contents of a file. This means that you get a live update of additions to the file as they arrive. This is useful for looking at log files as they change.
time will measure how much time it took to execute a command.
$ time say "All my people in the session, let me see your hands up!" real 0m6.248s user 0m0.223s sys 0m0.232s
grep stands for GNU regular expression parser. It can be used to search for words inside a file. It’s such a powerful tool that some consider grep to be a religion.
There are hundreds of other useful commands. At the end of the day, the command-line is your friend. Don’t be afraid to use the Terminal.
I’d be happy to hear what your favourite Terminal commands are!