September 19th, 2024
This page is intended to be a very brief tutorial on how to use a terminal, a.k.a. command-line or shell.
First, launch a terminal on your computer:
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
.
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.
mv
is short for move
and it can move and/or rename files or directories. It requires two arguments: what to move and where to move it. The destination can be a directory, in which case it puts the target into that directory, or a new (or existing) filename, in which case it renames the target to that name. To reiterate: if you specify the name of an existing file as the destination, that file will be deleted without warning and replaced by the target file.cp
is short for copy
and it will copy a file or directory instead of moving it. To copy an entire directory you need to specify the -R
flag which is short for ‘recursive’. Just like mv
, if the destination exists it will be overwritten.rm
is short for remove
and it will delete a file. It requires one argument: the file to delete. Use rm -R
to delete directories, but be extra careful with this!A few more things that bear mentioning:
./name
where “name” is the name of the program.-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.echo $PATH
to display the list of directories where the terminal will look for commands by default.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:
touch
echo
cat
less
pwd
clear
grep
find
python
vim
Note that not all of these may be available on Windows, which shares some core commands but has its own variants of many things.