CMSC 141 • HW 0, part 1 of 6
Linux is an operating system, like Windows or macOS. It lets you interact with your computer and gives you the usual tools, web browsers, editors, and so on. Most CS courses assume you can find your way around a Linux-like environment, and many ask you to run your code in one. The key skill is the terminal: a text window where you type commands to move around your files and run programs.
In this section you will learn the basics of working at the terminal: opening one, navigating the file system, finding help, and a few habits that make the command line faster and less frustrating.
If you have never used a terminal, plan to do this part during office hours so help is nearby.
Before anything else, you need a terminal window. The steps depend on your operating system.
wsl
application. Whenever these instructions say "terminal," you should be in a WSL window. Follow the
WSL setup guide to install it and connect your editor.Ctrl-Alt-T opens a terminal.When you are done, you should have exactly one terminal window open.
Your terminal shows a line that ends in a dollar sign, something like:
username@computer:~$
This line is the prompt. Whatever you type appears to the right of the $.
The program inside the window that reads your commands is called a shell. Most Linux
systems use bash; macOS uses zsh. For us they behave the same.
A terminal opens in your home directory, /home/username on Linux and WSL,
or /Users/username on macOS. A directory is the same thing as a folder.
$ to stand for the prompt. Do not type the $;
type only what comes after it. It is normal for your output to vary slightly from ours.
You will need a set of files to practice on. The download command depends on your operating system.
Each block does the same thing: move to your home directory, make a cmsc141 directory, move
into it, download a compressed file, and unpack it.
$ cd $ mkdir cmsc141 $ cd cmsc141 $ curl -O https://uchicago-cs.github.io/student-resource-guide/_static/linux-tutorial-files.zip $ unzip linux-tutorial-files.zip
$ cd $ mkdir cmsc141 $ cd cmsc141 $ wget --no-check-certificate -nv https://uchicago-cs.github.io/student-resource-guide/_static/linux-tutorial-files.zip $ sudo apt install unzip $ unzip linux-tutorial-files.zip
$ cd $ mkdir cmsc141 $ cd cmsc141 $ wget -nv https://uchicago-cs.github.io/student-resource-guide/_static/linux-tutorial-files.zip $ unzip linux-tutorial-files.zip
The Windows block has an extra step to install unzip; sudo will ask for your
laptop password. When the commands finish, your cmsc141 directory contains a
linux-tutorial-files directory to play with.
wget or curl commands you find online. Always confirm the
download site is one you trust before you run them.
Linux stores files in directories, which can hold files or other directories. The whole thing forms a tree, drawn with the root at the top. Your home directory is your own corner of that tree.
| Linux & WSL | macOS | |
|---|---|---|
| Root directory | / | / |
| Home directory | /home/username | /Users/username |
Two commands answer that: pwd ("print working directory") tells you where you are, and
ls ("list") shows what is in the current directory.
$ pwd /home/username/cmsc141 $ ls linux-tutorial-files linux-tutorial-files.zip
Try them yourself. The path from pwd and the list from ls are exactly what you
would see if you opened the folder in a graphical window; only the way you ask is different.
cd <path> | change into the directory path |
cd .. | move up one level |
cd | go to your home directory |
cd - | go back to the previous directory you were in |
To move into a directory, type cd followed by its name. Like most Linux commands,
cd stays quiet when it works; it only speaks up when the path is wrong.
$ pwd /home/username/cmsc141 $ cd linux-tutorial-files $ pwd /home/username/cmsc141/linux-tutorial-files $ ls Hello.java hello.c hello.cpp hello.py my-input.txt my_echo.py test.txt
To move back up one level, use cd .. Here .. always means "one directory up."
$ cd .. $ pwd /home/username/cmsc141
~ | shortcut for your home directory |
. | the current directory |
.. | one level up |
The paths above are relative: they describe where to go starting from where you are.
A path that begins with / is absolute; it starts from the root and always
leads to the same place no matter where you are. Running cd with no argument always returns
you home.
pwd, ls, and cd to navigate
into your linux-tutorial-files directory. The examples below assume you are there.
cp <source> <dest> | copy a file to a new name or location |
mv <source> <dest> | move or rename a file |
rm <file> | delete a file |
mkdir <name> | make a new directory |
cat <file> | print a file's contents to the terminal |
$ cp test.txt copy.txt
The destination can also be a directory, in which case the copy keeps its original name. Move
(mv) has the same form but does not keep the original. Remove (rm) deletes a file.
To copy or remove a whole directory and its contents, add the -r flag: cp -r or
rm -r ("recursive").
rm cannot be undone. Before you run rm -r on a directory, be sure you really
want everything in it gone.
A word like -r is called a flag; flags change how a command behaves. To
look at a short file without opening an editor, use cat:
$ cat test.txt Linux Tutorial - Test file ========================== Name: Firstname Lastname
For longer files, less shows one screen at a time (spacebar to page down, q to quit).
linux-tutorial-files:
test.txt to copy.txt, then ls to confirm both exist.copy.txt to copy2.txt with mv, and confirm with ls.backups.copy2.txt into backups, then list backups to confirm.copy2.txt from linux-tutorial-files.hello.py to the terminal with cat.Typing long names is tedious and error-prone, so the shell will finish them for you. Start typing a name and press Tab. If there is one match, it completes the name; if there are several, pressing Tab twice lists them. Getting in the habit of tab completion saves time and typos.
An asterisk * stands for any run of characters, which is handy for matching many files at
once. To list every file ending in .txt:
$ ls *.txt
rm. The command rm * deletes every
file in the current directory. Do not run it.
Text after a # on the command line is a comment and is ignored.
cmsc141 directory, run
ls linux-tutorial* and then ls linux-tutorial*/*.py. What is the difference?
Two ways to get help from inside the terminal: the --help flag and man pages.
Many commands accept a --help flag that prints a short summary:
$ pwd --help
A man page (manual page) is the full documentation for a command: what it does, which flags it accepts, and how to use it. To read one:
$ man ls
Man pages are thorough but dense; reading them is a skill you build with practice. The -k
flag searches page descriptions by keyword:
$ man -k printf
ls hides files whose names begin with a dot.
The linux-tutorial-files directory has one such file. Use man to find the
ls flag that lists hidden files.
When we write Ctrl-X, we mean hold the Control key and press X at the same time. You do not
need Shift even though we capitalize the letter.
If a program runs away or hangs, Ctrl-C sends it an interrupt and usually stops it; you may
need to press it more than once. Ctrl-D sends an end-of-input signal, which is useful when a
command like cat is waiting for input.
Your past commands are saved. Press the up arrow (or Ctrl-P) to step back through them and
the down arrow (or Ctrl-N) to go forward. Some line-editing shortcuts:
Ctrl-A | jump to the start of the line |
Ctrl-E | jump to the end of the line |
Ctrl-U | erase from the cursor back to the start |
Ctrl-K | erase from the cursor to the end |
Ctrl-L | clear the screen |
Recalling and editing previous commands instead of retyping them saves real time. Like tab completion, it is worth building the habit now.
Next: Introduction to VSCodium →