← HW 0 overview

Introduction to Linux

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.

macOS and Windows WSL behave almost identically to Linux for our purposes, so we will say "Linux" to cover all three. Where the commands differ, we say so.

If you have never used a terminal, plan to do this part during office hours so help is nearby.

Opening a terminal

Before anything else, you need a terminal window. The steps depend on your operating system.

When you are done, you should have exactly one terminal window open.

What you are looking at

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.

From here on, we use a single $ 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.

Getting the practice files

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.

macOS

$ 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

Windows (WSL)

$ 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

Linux

$ 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.

Be careful running wget or curl commands you find online. Always confirm the download site is one you trust before you run them.

Navigating the file system

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 & WSLmacOS
Root directory//
Home directory/home/username/Users/username

Where am I?

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.

Changing directories

cd <path>change into the directory path
cd ..move up one level
cdgo 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.

Try this! Use pwd, ls, and cd to navigate into your linux-tutorial-files directory. The examples below assume you are there.

Useful commands

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).

Try this! Inside linux-tutorial-files:
  1. Copy test.txt to copy.txt, then ls to confirm both exist.
  2. Rename copy.txt to copy2.txt with mv, and confirm with ls.
  3. Make a directory called backups.
  4. Copy copy2.txt into backups, then list backups to confirm.
  5. Remove copy2.txt from linux-tutorial-files.
  6. Print 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.

Wildcards

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
Be very careful combining wildcards with 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.

Try this! From your cmsc141 directory, run ls linux-tutorial* and then ls linux-tutorial*/*.py. What is the difference?

Getting help

Two ways to get help from inside the terminal: the --help flag and man pages.

--help

Many commands accept a --help flag that prints a short summary:

$ pwd --help

man pages

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
Exercise. By default, 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.

Tips and tricks

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-Ajump to the start of the line
Ctrl-Ejump to the end of the line
Ctrl-Uerase from the cursor back to the start
Ctrl-Kerase from the cursor to the end
Ctrl-Lclear the screen

Recalling and editing previous commands instead of retyping them saves real time. Like tab completion, it is worth building the habit now.