Technical Literacy Command Line Mac
Terminal
Access the Mac command line by starting Terminal, which you can find in your list of applications. Once started, you can create new Terminal windows with CMD+N, or a new tab with CMD+T. Close a tab or window with CMD+W.
Command Prompt
When you first open the Terminal, you might see something like:
AlicesMac:~ alice$
This is called the command prompt. You'll see a blinking vertical line, called the cursor. This shows where the characters you type will appear, and also let you know the prompt is ready to receive input (if a program is running, you won't see it).
The prompt also gives you some useful information. Here, we can see that we're on a computer called AlicesMac, we're in the ~
(home) directory, and username is alice.
Have a look at your command prompt now and see if you can extract these details from your own prompt.
At the command prompt, you type (or paste in) a command which runs when you press ENTER.
Running programs, specifying options
Normally at the terminal you run programs or scripts which do something for you. For example, to count the number of words in a file. Because programs often need some kind of input (such as a name of a file or directory to process) we have to provide parameters (or arguments or options).
It's typical that this essential parameter - the thing you want the program to work with - is typed after the name of the program. So to use the program wc
to show the number of words in the file 'essay.txt', we could try something like the following:
AlicesMac:~ alice$ wc essay.txt
1 2 10 essay.txt
All we really wanted to do is find out the number of words, so let's find out if we can narrow down wc
's output. To do so, we need to turn to the documentation. OS X has in-built man pages (manual pages) that are useful for getting quick information on a command. Just give man
the name of the command you're interested in:
man wc
Running this, we get pages of information about the wc
command (shown here excerpted):
SYNOPSIS
wc [-clmw] [file ...]
...
The following options are available:
-w The number of words in each input file is written to the standard output.
...
When an option is specified, wc only reports the information requested by that option.
The man page gives us some clues as the options we can provide it, and how we use them. -w
looks useful, so we can try that, putting it before the filename as the manpage shows us:
AlicesMac:~ alice$ wc -w essay.txt
2 essay.txt
Now the result is more succinct: essay.txt has two words in it. (time to get cracking!)
Paths
In OS X, the root (or utmost top-level) of your file system has the path of /
. Each sub-directory, including your personal files, lives somewhere down in the file system tree. When you're accessing files in the Finder, you'll typically only work with files within your home directory, which we can refer to at the command line as ~
. If you're curious as to what the actual path of your home directory is, you can change to it using cd ~
and then using pwd
to display your current location.
Each file and directory has a unique path. The path is made up of the location within the filesystem tree as well as the name of the file or directory. Eg, my music directory might have the name 'music', and be located at /Users/alice
, therefore have the absolute path of /Users/alice/music
.
Tips
- When you are working with directories in a graphical user interface, they will likely be referred to as a folder. Same thing, different name and representation.
- If you have a file or directory selected in Finder, hit CMD+I to open the Inspector and see its path
- As you change directory, type the first letter of a path and press the TAB button on your keyboard. This allows you to 'tab complete' the path - a way of avoiding typing and also to probe out a path name
Relative paths
Any path that doesn't start with a forward slash is a relative path, meaning that its actual location depends on where you are when you refer to it. Relative paths are essential because otherwise we'd always have to type absolute (or full paths. Note that ~
points to your home directory's absolute path.
For example, in my home directory, let's say I have documents, music, and photos subdirectories. If am at my home directory, I can type cd music
to change into the music subdirectory (so I will be at ~/music
). When I use the relative path 'music' as part of cd music
, the system needs to resolve it to an absolute path. In this case, because there is a subdirectory called 'music' in our current directory, it knows what we mean by that relative path.
If the system can't resolve a relative path, you'll get an error message along the lines of 'No such file or directory'.
You can traverse as many levels down the filesystem tree as you like:
cd ~/music/r/radiohead/amnesiac
You'll also want to go 'up' the filesystem tree, to shift your location to the parent directory of the directory you are in. You can use the special reference ..
for this. ..
always means 'up one level from the current path reference'.
For example, if we're deep down in our music:
AlicesMac:amnesiac alice$ pwd
/Users/alice/music/r/radiohead/amnesiac
AlicesMac:amnesiac alice$ cd ..
AlicesMac:radiohead alice$ pwd
/Users/alice/music/r/radiohead
You can use several of these references to jump multiple levels up:
AlicesMac:amnesiac alice$ pwd
/Users/alice/music/r/radiohead/amnesiac
AlicesMac:amnesiac alice$ cd ../../../
AlicesMac:music alice$ pwd
/Users/alice/music
Another handy reference to know is the single period .
which refers to your current location. In many cases it is not necessary to use it, but there are exceptions like when you start a program in the current directory.
Orientation
At the command line, your activity and the commands you run are in relation to the "current working directory". That is it say, you are always "standing" somewhere in your filesystem.
At any point, you can also run the pwd
(print working directory) command to find out where you are:
AlicesMac:~ alice$ pwd
/Users/alice
AlicesMac:~ alice$
There is also a notion of home, an area of the filesystem for your own files. This is where your documents, music etc. normally live as well. In the Finder, you can get to your home directory via the Favourites list on the left of the Finder window, or via Finder's menu (Go > Home).
At the command line, the tilde character (~
) always refers to your home path, and you can use it where ever you might normally use a full path.
For example, to jump to your home directory:
cd ~
Listing Files
ls
(list) displays the files and directories. If you don't give ls
a path, it will display the contents of the current directory.
Examples
- List the current directory:
ls
- List the /Users directory
ls /Users
ls
has many options for which entries should be shown and how. Useful options are la
, which list all entries with detail (size, date modified, etc):
- List details of current directory
ls -la
Working with directories
Changing directory
The cd
(change directory) command is used to, well, change directory. This allows you to change where you are 'standing' at the command line.
Examples
- Change to your home directory:
cd ~
- Change to your documents directory:
cd ~/Documents
- Change to an absolute path:
cd /Users/bob/Music
Making
mkdir
will make a directory
Examples
- Make a directory 'test' in the current directory
mkdir test
- Make a directory 'test' at a specific location
mkdir /Users/alice/documents/test
Deleting directories
rm -rf
(rm: remove) will delete a directory.
Examples
- Delete a directory 'test' in the current directory
rm -rf test
- Delete a specific 'test' directory
rm -rf /Users/alice/documents/test
Note: 'rm -rf' is dangerous - it will delete all files and subdirectories without any prompt or ability to undo. Be very sure of the path you have told rm to remove before pressing ENTER
Working with files
Copying
Use cp
(copy) to copy. You have to provide a source and destination path.
cp test.txt copy.txt
cp test.txt /Users/alice/documents/copy.txt
You can copy entire directories (and their contents) with the -R
(recursive) option.
cp -R music music2
Moving
Use mv
(move) to move files and directories. You have to provide a source and destination path.
mv test.txt copy.txt
mv test.txt ../
You can also move directories (and their contents).
mv music music2
mv music old/
Deleting
Use rm
(remove) to move files and directories.
rm test.txt
To remove directories and their contents, include the -rf
option.
rm -rf music
Elevated permissions
Some commands need a higher level of access to run. If this is the case, you can prefix the command you run with sudo</span>. For example, sudo ls</span>, rather than just ls</span>.
Be very careful about using sudo. Any command you run with it will be able to do whatever it likes to your computer. Any mistakes you make, for example in deleting or moving files, could have very drastic outcomes if you run it with sudo. It should be very uncommon that you need to use it.
Tips
- Pressing the up and down arrow keys on your keyboard allow you to flip through commands you've previously typed.
- If you want more power and customisation, consider downloading and using iTerm2
- You can drag and drop a folder from Finder to the Terminal to paste its path.
- You can stop a currently running command with CTRL+X (forcing it to exit).
exit
will close the Terminal session.
Useful commands
whoami Displays your username
uname -a Displays details of your computer
clear Clears the contents of the Terminal window
more filename Displays the contents of a file (only try this with text files)
If you want to go deeper into Terminal-based work, install Homebrew to simplify managing command-line software.