Terminal Basics

Peter Mawhorter (pmawhort@wellesley.edu)

September 19th, 2024

Terminal Basics

This page is intended to be a very brief tutorial on how to use a terminal, a.k.a. command-line or shell.

Getting Started

First, launch a terminal on your computer:

  1. On a Mac, launch the “Terminal” app.
  2. On Windows, run “PowerShell”.
  3. On Linux, there’s usually a “Terminal” app, or you’re in a shell by default.

When the terminal starts up, you’ll see a blank screen with a bit of text at the top. This is your prompt. The terminal accepts a text command, runs it, and then asks for another, until you tell it to quit. Type exit and hit enter if you want to quit.

The first command you need to know is man, short for manual (most common commands are 2-3 letter abbreviations because it saves time typing).

Go ahead and type:

man man

and then hit enter now. This will bring up the manual for the man command. When reading a manual, you can use the arrow keys to scroll up and down, you can type ‘/’ to initiate a search, and you can hit ‘q’ to quit.

Whenever you want to know more about another command, use man.

Core Commands

The two most essential commands are cd and ls. The terminal always tracks a ‘current directory’. Everything you do like creating or running files will reference files in the current directory by default. So to do anything useful, you need to be able to change directories (cd) and see what files are in the current directory (ls).

Try it out by using

ls

now. You should see a list of files in what’s called your ‘home directory’ which usually includes folders like “Desktop”, “Downloads”, and “Documents”.

Assuming you have a “Desktop” folder, you can run cd Desktop (case matters) to change into that directory. Typing ls again afterwards should show you the files on your desktop.

If you want to go back up to an outer directory use cd ... You can change multiple directories at once by separating names with /, so for example, cd ../.. would take you up two directories in one command.


At this point, it’s worth mentioning that the command line operates on a word-by-word basis, using spaces to separate parts of a command. So cd .. has two parts: cd and ... The first word you type is always the command to run, subsequent words mean different things depending on the command. We call these words ‘arguments’.

The ls command doesn’t need any arguments, it just lists the contents of the current directory. But if you give it an argument, it will list the contents of that directory instead. Try running

ls ..

and see what it shows you.

In addition to arguments, most commands accept one or more “flags” (also called options). We write these with either one or two dashes followed by a letter or word. The manual page for a command will tell you what options are available. Try doing:

ls -l

The -l option here specifies ‘long’ format, which shows you more details about each entry.


Three more core commands are mv, cp, and rm. They’re all a bit dangerous, so be careful: if you supply a destination which already exists, mv or cp will overwrite that file and delete its contents without warning, and rm does not warn you before deleting things.

A few more things that bear mentioning:

  1. If there is an executable program in the current directory, you can run it by typing ./name where “name” is the name of the program.
  2. If the terminal ever freezes up, you can hit control-C to interrupt the current process (even on a Mac you use the control key for this, not command).
  3. In addition to manual pages, most commands accept a -h and/or --help option. Using this will cause them to print out a help message. For example, cp --help will print help for the cp command.
  4. When installing new terminal programs, you may need to start a new terminal in order to use them. You can use the command echo $PATH to display the list of directories where the terminal will look for commands by default.

More

There are many more interesting terminal commands, but these are enough to get started. This tutorial is supposed to be a short tutorial. A few that you might want to look up via man include:

Note that not all of these may be available on Windows, which shares some core commands but has its own variants of many things.