Terminal

From Mac Guides

Jump to: navigation, search
The Terminal application.
Enlarge
The Terminal application.
The Terminal is a program included with all versions of Mac OS X. It is located in the Utilities folder within the Applications folder. When launched, it provides a line interface to control the underpinnings of the UNIX based operating system.

The default UNIX shell in Mac OS X Panther (10.3) or later is Bash

Contents

Commands

Here is a list of common commands:

ls 
list files and directories
cd 
change directory
mkdir 
create a new directory
cp 
copy files or directories
mv 
move (rename) files or directories
rm 
remove files or directories

See the Terminal Commands category for a list of useful Terminal programs.

A few basics

When the Terminal is first opened, a message similar to the following appears:

 Last login: Sat Dec 17 14:08:02 on ttyp2
 Welcome to Darwin!
 h460db012:~ markyoung$ 

Line 1 shows the last time the Terminal was used. 'ttyp2 can be thought of as the "connection" to the Terminal. For example, opening another terminal window would probably show "ttyp3". Alternatively, if you have enabled Remote Login, the remote address of the last user would be visible there. If the date or last connection is unfamiliar, it could indicate someone has unwanted access to your computer.

Line 2 is just a friendly greeting from Darwin.

Line 3 is the prompt, or command prompt. It shows the machine name (h460db012), a colon, the present working directory (~) a space, the current user (markyoung) and a dollar sign (or a hash (#) if the shell has super user privileges).

The current directory

The current directory, also known as the working directory, is where you are. In the Finder, this is equivalent to having a window open and viewing the files. To determine the current directory, type in:

pwd

Which produces an output similar to this:

 h460db012:~ markyoung$ pwd
 /Users/markyoung

pwd stands for "present working directory", and will likely output "/Users/(yourusername)". You may also notice that the current directory is displayed in a different form in the prompt. In this case, "~" is equivalent to "/Users/markyoung", the home folder. To change the current directory, use the cd command, as in

 h460db012:~ markyoung$ cd Documents/
 h460db012:~/Documents markyoung$ pwd
 /Users/markyoung/Documents

You are now in your Documents directory, just as if you had double clicked on the Documents folder in the Finder. Note how the prompt has changed to include the path "~/Documents". You can get a list of all the documents in this directory with the ls (list) command:

 h460db012:~/Documents markyoung$ ls
 Cocoa Programming
 Mars.snf
 Microsoft User Data
 Pirate Girl.jpeg
 jeep dimensions.rtf
 jeep stuff.rtf
 macTV_SteveJobsSNLSkit.m4v
 starwarsWhosOnFirst.wmv

The ls has many, many options, and is worth learning. For example, ls -F will postfix a slash (/) on every directory and an asterisk (*) on every executable file.

To open one of these documents, use the open command. The open command is equivalent to double clicking a file in the Finder.

 h460db012:~/Documents markyoung$ open starwarsWhosOnFirst.wmv 

Job control

While Activity Monitor is useful, it's not quite as useful as working with the shell.

To get a list of every running process on your computer, type this:

 ps -ax

"process status" dumps out a long list of everything that's running, along with the job number. Paired with the grep command, you can quickly determine if a process or application is running. Example,

 h460db012:~/Documents markyoung$ ps -ax | grep http
 164  ??  Ss     0:06.05 /usr/sbin/httpd
 1377  p1  R+     0:00.00 grep http

"/usr/sbin/httpd" confirms that Apache is running.

 h460db012:~/Documents markyoung$ ps -ax | grep mysql
  1379  p1  R+     0:00.00 grep mysql

In this case however, MySQL is not running. The only process that matches "mysql" is the grep process itself (the process doing the searching).

To forcefully end a process, kill it.

 h460db012:~/Documents markyoung$ ps -ax | grep Dock
  91  ??  S      0:15.03 /System/Library/CoreServices/Dock.app/Contents/MacOS/Dock -psn_0_393217
  1383  p1  R+     0:00.00 grep Dock
 h460db012:~/Documents markyoung$ kill 91

Here, we first find the process ID of the Dock, then use the kill command with the process ID as a parameter. Alternatively, you can skip the process list and grep, and use the "combined" form:

 h460db012:~/Documents markyoung$ killall Dock

Of course, the "kill" command can cause some issues if you kill the wrong process id.

For more on jobs and job control, see the jobs article.

Who Am I and Where Am I

To get the name of the current user, use the cleverly named "whoami" command (although this information is also available in the command prompt).

 h460db012:~/Documents markyoung$ whoami
 markyoung

To get your current location, use the "hostname" command.

 h460db012:~/Documents markyoung$ hostname
 h460db012.area2.spcsdns.net

Some Other Useful Tips

Home Directory shortcuts

Your home directory (/Users/[yourusername]) can quickly be navigated to by typing "cd" with no arguments. Alternatively, the tilde(~) character is a shortcut for the home directory of the logged in user. The tilde can prefix another user's name to become the shortcut of that user's home directory. Therefore, these commands are equivalent (assuming markyoung is the current user):

cd ~
cd ~markyoung
cd /Users/markyoung

Auto-completion

At any time, when typing a command, the Terminal (or, more accurately, bash) allows for filename completion, by hitting the tab key. As a common example, type this

 tail -f /var/log/sys

But, before hitting return, hit "tab". The shell will complete the filename, as the only thing in the /var/log directory that starts with "sys" is "system.log". If there's multiple matches, the shell will complete it up to the last common point. For example:

 tail -f /var/log/sec 

Tab in this case merely completes up to /var/log/secure.log and then beeps. This is because there are (probably) multiple files that begin with "secure.log". Pressing tab a second time will tell the shell to provide a list of matching files. For example:

 h460db012:~/Documents markyoung$ tail -f /var/log/secure.log
 secure.log       secure.log.1.gz  secure.log.3.gz  
 secure.log.0.gz  secure.log.2.gz  secure.log.4.gz  
 h460db012:~/Documents markyoung$ tail -f /var/log/secure.log

This handy feature allows you to choose the one you want. If you were to type ".1", then tab, then it would complete the filename as "secure.log.1.gz" (although in this example this is a gzip'd file, which isn't tailable)

Drag and drop

To save time typing out a long path, you can drag an icon from Finder to a Terminal window, and the whole path will be filled in for you. For example, if you wanted to set the current directory to ~/Library/Preferences/, you could type in (with a trailing space):

cd 

Then drag the Preferences folder onto the Terminal window, press return, and the command will work as normal. The icon in the title bar of a Finder window is also draggable in this manner.

See Also