Command Line Usage
Opening the terminal
Opening iTerm2 (Mac only)
1.
2.
Opening the terminal in Visual Studio Code (Mac and Windows)
1.
2.
3.
The command prompt
The command prompt is the name given to the line in the terminal where the user can provide input ('prompt' is another way of saying 'to ask for input'). In the Mac terminal, the command prompt usually includes the name of the logged-in user and the name of the computer (separated by an @
sign), followed by the current directory. This can be customized fairly easily.
Entering commands in the shell
Spaces
Commands, arguments, and options should all be separated by spaces or other whitespace characters. Otherwise, the terminal will treat them as one word.
If a single argument includes spaces or whitespace (this is the case with git
commit
messages and when directory names include spaces), surround the whole thing with quotation marks ("
or '
). For example:
cd "My Photos"
will navigate to a directory named "My Photos"
, but:
cd My Photos
will produce an error.
Running commands
To use a command from the command line, enter the name of the command, followed by any required arguments, followed by any options you want to include in the command. Some options require additional arguments.
cal
command
Extended examples using the cal
is short for 'calendar' (many terminal commands are abbreviations or truncations), and it can be used to show calendar information in
the terminal. The cal
command has several options that alter the output it shows; we look at a few of them below, as an introduction to the usage of options and arguments.
With no arguments or options, cal
prints the current monthWith a single argument, cal
prints the year indicated by the argumentWith the option -y
,cal
prints the full calendar yearWith the option -m
followed by an argument,cal
prints the corresponding month of the current yearWith multiple arguments (each separated by a space), `cal` interprets the first as a month and the second as a year, printing the corresponding month
Terminal and directory structure
Unix
Unix refers to a family of operating systems developed by researchers at AT&T in the 1970s. Many modern operating systems, including Mac OS X, are derived from Unix. Unix and Unix-like systems are known, among other things, for their distinctive file system and extensive set of command line utilities.
Files and directories
Most modern operating systems organize data using a file system. In a typical file system, data is organized into files and directories. Files are named locations where data can be written. Directories (you're probably used to calling them folders) are just collections that can contain files or other directories.
Working directory
An open terminal is always tied to a particular directory in the computer's file system. This directory is often called the current directory or working directory. When commands To navigate to different directories in the file system,
we use the cd
command.
The home directory
The home directory is the name of a special directory created for each user account on the computer. The home directory contains installed applications, photos, project folders, and other data specific to that user. It's also the initial working directory when a new terminal is opened. On
Paths
A path is a string of file and directory names separated -- by the /
character -- that can be used to access that file. /Users/momentum/assignments/my_assignment.js
is an example of a path on a machine running Mac OS X.
Absolute path
An absolute path begins with the /
character (representing the root directory). An absolute path is the full path from the root directory to another file or directory on the computer.
Relative path
A relative path does not begin with /
. A relative path is the path from the working directory to some resource in the working directory or one of its subdirectories. templates/account/login.html
is an example of a relative path. If the working directory is /Users/momentum
, the absolute path corresponding to the above relative path is /Users/momentum/templates/account/login.html
.
Special directory names
The following special symbols are frequently used in conjunction with the cd
command to navigate the file system. These symbols can be combined with each other and with regular directory names (where sensible), so ~/templates
and ../../images/my_pic.png
are valid directory names.
'~'
This special symbol refers to the home directory. It's used to navigate back to the home directory, or specify a path starting in the home directory.
'.'
This special symbol refers to the current directory.
'..'
This special symbol refers to the parent of the current directory (the directory this directory is contained within). It's used very frequently when navigating directories with cd
.
'-'
This special symbol refers to the last directory we were in before we used cd
to navigate to the working directory. Think of this as a 'back button' for the terminal. Whatever directory we're in now, cd -
will navigate us to whatever directory we were in right before we navigated to this one.
Exercises
Try using the
curl
command with the URL for this page. What output appears in the terminal?Try running the command
man cd
. What output appears in the terminal?Try running the command
mkdir --help
. What output appears in the terminal?