Tutorial - Linux Basics

Linux is an operating system, just like Windows and MacOS. It allows you (the user) to interact with your computer, and provides many of the same tools you’re accustomed to in Windows and MacOS (web browsers, word processors, etc.)

In almost all CS classes, instructors will assume that you know your way around a Linux environment, and may require that you compile and run code in a Linux system. While there are many things you’ll be able to do from a Linux desktop environment, it is also important that you be come comfortable using the terminal, a command-line interface for interacting with the filesystem, running programs, etc.

In this tutorial you will learn how to use the terminal to perform some basic operations in Linux, including how to:

  1. Use basic terminal commands

  2. Navigate a UNIX filesystem

  3. Edit, compile, and run a program

  4. Use keyboard shortcuts.

Where should you do this tutorial?

Since one of the goals of this tutorial is for you to be able to use the CS department’s Linux environment, we strongly suggest you work through this tutorial on a UChicago CS software environment. While you can work through the tutorial in one of our computer labs, we recommend using SSH to log into a CS Linux server from your personal computer, as this will likely be the primary way you’ll be interacting with the CS department’s Linux systems.

It should also be possible for you to work through this tutorial in other UNIX environments, such as the MacOS terminal or Ubuntu WSL on Windows, but we recommend against it, since you should try to become familiar specifically with the Linux environment provided by the CS department.

Terminal/Shell

On your personal computer, you probably navigate your hard drive by double clicking on icons. While convenient for simple tasks, this approach is limited. For example, imagine that you want to delete all of the music files over 5 MB that you haven’t listened to in over a year. This task is very hard to do with the standard double-click interface but is relatively simple using the terminal.

If you are using a desktop environment (e.g., you are sitting in CSIL or you’re using the Virtual Desktop), you can start a terminal by clicking on the Application icon (3x3 grid of dots) at the bottom left of the screen:

Application icon in Ubuntu

Then, type “terminal” in the input box. Click the “terminal” icon to open a terminal window. You can also use the keyboard shortcut: Ctrl+Alt+t.

If you are using SSH, connecting to a CS Linux server will directly open a terminal for you.

Regardless of how you open the terminal, you will see something like this:

username@computer:~$

where username has been replaced by your CNetID and computer is the name of the machine you happen to be using. This string is called the prompt. When you start typing, the characters you type will appear to the right of the $.

The program that runs within a terminal window and processes the commands the you type is called a shell. We use bash, which is the default shell on most Linux distributions, but there are other popular shells, such as ksh, tcsh, etc.

In the remaining sections, we will introduce a new concept or skill in each section, and will provide a few simple examples. In fact, we show many examples of sample output throughout the tutorial. Bear in mind that the output you see when you run through our examples may vary a bit; this is normal. We have also included a few exercises in each section so you can practice those skills.

Man Pages

A man page (short for manual page) documents or describes topics related to working with Linux. These topics include specific Linux programs, certain programming functions, standards, and conventions, and abstract concepts.

To get the man page for a Linux command, you can type:

man <command name>

So in order to get the man page for ls, you would type:

$ man ls

This command displays a man page that gives information on the ls command, including a description of the command, a list of the flags it supports, instructions on how to use it, and other information.

Each man page has a description. The -k flag for man allows you to search these descriptions using a keyword. For example:

$ man -k printf

This searches all the descriptions for the keyword printf and prints the names of the man pages with matches.

Learning how to read man pages is an important skill.

Exercise

By default, the ls command does not include files with names that start with a dot (.). The linux-tutorial-files directory contains a file that starts with a dot. Use man to identify the flag to use with ls to include this file when listing the contents of linux-tutorial-files.

Editing files

In many of your classes, you will have to edit text files containing programming code. While there are many graphical editors you could use (either on a CS Linux environment, or on your own computer), it is also important to be familiar with terminal editors that run exclusively from a terminal and don’t require a desktop environment. These editors can be particularly useful if you only have access to a terminal (e.g., if you’re logging into a CS Linux server using SSH).

Using a terminal editor

List the files in the linux-tutorial-files directory. You should see the following:

backups  hello.c  hello.cpp  Hello.java  hello.py  my_echo.py  my-input.txt  test.txt

Let’s say we wanted to edit the file test.txt. There are many different terminal editors we could use, but we will start with a simple and fairly intuitive one: nano. To edit the file, run the following:

$ nano test.txt

This will open the test.txt file in the nano editor, which will look something like this:

../_images/nano.png

The way you interact with this editor will be very similar to how you use a text editor (or a word processor) in a graphical desktop environment: you can use the arrow keys to move around the text, and typing text will insert that text at the location of the cursor. You can also use the Backspace key to delete text.

Try removing the text Firstname Lastname and replacing it with your name. Then, save the file by pressing Ctrl-O (i.e., the Control key and the O key at the same time). You will see the following prompt at the bottom of the screen:

File Name to Write: test.txt

You can just press Enter to confirm you’d like to save the changes to the same file (however, you could also specify a different file).

The bottom of the screen actually specifies some of the most common commands you can run in the editor. For example, ^O Write Out refers to what we just did: Pressing Ctrl-O allows your “write out” (i.e., save) the file (a common abbreviation for the Control key is ^).

Another common command is ^X Exit. Just press Control-X to exit the editor.

While nano is a simple and intuitive editor, there are many other editors out there. If you’re interested in a more powerful terminal editor, you may want to check out Vim or Emacs.

Using a graphical editor

A graphical editor requires a desktop environment so, if you have been working through this tutorial using SSH, you should skip this section, as SSH won’t allow you to run graphical programs.

If you are using a desktop environment (e.g., if you are logging into a CSIL computer, or using the UChicago CS Virtual Machine or Virtual Desktop), you can try out Visual Studio Code to see an example of what a graphical editor looks like.

You can open a specific file, say hello.py, using the code command from the Linux command-line by typing:

$ code hello.py

When you run this command, you will get a new window that looks like this:

Specifically, you’ll see the following text:

print("Hello, world!")

If the file is blank, quit code and ensure that the file hello.py exists in your local directory (use ls to list the files in your local directory). If it does not, use cd to navigate to the linux-tutorial-files directory.

For now, we will use Visual Studio Code (code) in a very basic way. You can navigate to a particular place in a file using the arrow keys (or your mouse) and then type typical characters and delete them as you would in a regular text editor. You can save your changes using the Save option in the File menu or use the keyboard shortcut Ctrl-s. To quit, you can use the Exit option in the File menu or the keyboard shortcut Ctrl-q.

As an aside, you can also launch code from the application launcher: simply click the Application button (at the top left of your screen), type “code” in the input box, and then click on the Visual Studio Code icon. You can then use the “Open File…” option in the File menu to open the correct file.

The edit/compile/run cycle in the terminal

When writing code, you will very often go through several cycles of the edit/compile/run cycle:

  1. Edit: You edit the source code file to add or modify some code.

  2. Compile: You compile the code into a runnable executable (only in compiled languages; e.g., this step doesn’t apply in Python).

  3. Run: You run the executable to verify that the code you added/modified works as expected.

We have previously covered how to edit files from the terminal, but now we’ll see the basic commands to compile and run your code from the terminal. We have included four example programs in the tutorial files which you can use for this purpose:

  • hello.py (Python)

  • hello.c (C)

  • hello.cpp (C++)

  • Hello.java (Java)

Python

In Python, given a .py file, such as our hello.py file, we can run it from the terminal like this:

$ python3 hello.py
Hello, world!

Exercise: Try editing the file (e.g., change the message from Hello, world! to Hello, universe!) and running the program again. You should now see the updated message.

C

C is a compiled language, which means that we first need to compile our program to produce an executable file. For example, we can compile our hello.c program like this:

$ gcc hello.c -o hello

We are using the gcc compiler, but some classes may use the clang compiler. The first parameter (hello.c) specifies the C file we want to compile, and the -o option specifies the executable file we want to produce.

Running the above command will produce a hello file that you can run like this:

$ ./hello
Hello, world!

Exercise: Try editing hello.c (e.g., change the message from Hello, world! to Hello, universe!). If you re-run ./hello, you’ll see that the old message is still being printed out: this is because you need to compile the hello.c file to produce an updated executable. Once you do so, you should see the updated message when you run ./hello

C++

The process for compiling/running programs in C++ is basically the same as in C, except we will use the g++ compiler:

$ g++ hello.cpp -o hello++
$ ./hello++
Hello, world!

Java

Like C/C++, Java is a compiled language, although the Java compiler doesn’t produce an executable in the same way that the C/C++ compiler does (we’ll see why momentarily).

To compile a Java file, you need to run this:

$ javac Hello.java

Unlike the C/C++ example we just saw, this will actually produce a file called Hello.class that is not directly runnable from the terminal (i.e., running ./Hello.class like we did in the C/C++ example won’t work). Instead, we need to use the java command to run it:

$ java Hello
Hello, world!

Notice how we don’t have to include the .class extension.

Exercise: Try editing Hello.java (e.g., change the message from Hello, world! to Hello, universe!). If you re-compile the file and run it again, you should see the updated message.

Tips and Tricks

Terminating a program

Sometimes, a program will run indefinitely or misbehave. When this happens, you can type Ctrl-C to send an interrupt signal to the running program, which usually causes it to terminate. On occasion, you may need to type Ctrl-C a few times. As noted earlier, typing Ctrl-D sends an end of input signal, which tells the program that no more information is coming.

Keyboard shortcuts

Used in the terminal, the keyboard shortcut Ctrl-P will roll back to the previous command. If you type Ctrl-P twice, you will roll back by two commands. If you type Ctrl-P too many times, you can use Ctrl-N to move forward. You can also use the arrow keys: up for previous (backward), down for next (forward).

Here are few more useful shortcuts:

  • Ctrl-A will move you to the beginning of a line.

  • Ctrl-E will move you to the end of a line.

  • Ctrl-U will erase everything from where you are in a line back to the beginning.

  • Ctrl-K will erase everything from where you are to the end of the line.

  • Ctrl-L will clear the text from current terminal

Play around with these commands. Being able to scroll back to, edit, and then rerun previously used commands saves time and typing! And like auto-completion, getting in the habit of using keyboard shortcuts will reduce frustration as well save time.

Acknowledgments

Parts of this tutorial are based on a Linux lab originally written for CMSC 12100 by Prof. Anne Rogers and Prof. Borja Sotomayor, and edited by numerous instructors and TAs over the years.