Operating Systems 2026F: Tutorial 1: Difference between revisions
| Line 124: | Line 124: | ||
<ol> | <ol> | ||
<li>Can you find any files where the size is zero but the file clearly contains data? What filesystem(s) contain such files?<li> | |||
<li>When you have logged in to a shell, how (i.e., using what commands?) do you first find out information about the environment? | <li>When you have logged in to a shell, how (i.e., using what commands?) do you first find out information about the environment? | ||
<ol style="list-style-type:lower-alpha"> | <ol style="list-style-type:lower-alpha"> | ||
| Line 143: | Line 144: | ||
</pre> | </pre> | ||
</li> | </li> | ||
<li>Run the following command. What is the value of $x afterwards? What if you interrupt it in the middle, what is the value of $x? How does this differ from the answer to Question | <li>Run the following command. What is the value of $x afterwards? What if you interrupt it in the middle, what is the value of $x? How does this differ from the answer to Question 6? | ||
<pre> | <pre> | ||
(for x in 1 2 3 4 5 6 7 8 9 10; do echo $x; sleep 1; done) | (for x in 1 2 3 4 5 6 7 8 9 10; do echo $x; sleep 1; done) | ||
Revision as of 17:32, 14 September 2026
THIS TUTORIAL IS STILL IN DEVELOPMENT.
In this tutorial you will be learning the basics of command-line interaction in Linux.
Getting Started
For this tutorial, you need to get access to a Linux or UNIX machine. In the next tutorial we'll start using Openstack, but for this tutorial we will instead use LinuxOnTab, a version of Linux that runs in a browser tab.
To do the following, please go to https://next.linuxontab.com, give it a few moments to load, and then proceed with the following.
Note that by default this linux instance is ephemeral, so don't keep anything you care about in it - it will go away!
Background
LinuxOnTab
LinuxOnTab is an open source project developing a version of Linux that can run inside of a browser tab. The original version included an emulator of an x86-compatible processor which allowed it to run the same Linux programs that would run on most PCs. We, however, are using the new version which is based on WebAssembly.
In this tutorial you'll learn both about Linux and about this WebAssembly-specific version; in Tutorial 2 you'll learn about a more conventional cloud Linux system.
The Shell
The shell or command line provides a text interface for running programs. While not as visually pleasing as a graphical interface, the shell provides a more clear representation of the functionality provided by the operating system.
To run a program contained in the current directory in the shell, you need to prefix the name of the command with a ./. This "./" tells the shell that the location of the command you wish to run is the current directory. By default, the shell will not search for executable commands in the current working directory. To run most system commands, the name of the command can be typed without a path specification.
Note that there are many kinds of shells, and people can be very opinionated about which shell is best. We will be normally using bash, but there are many others including ones that have been around forever (sh, csh, tcsh) and somewhat newer, more feature-filled shells (ksh and zsh). There are also shells that were first built for non-UNIX-like systems but now run on Linux (Powershell). Wikipedia has a nice article comparing the features of different command shells.
Shell Basics
Note that bash is the default shell on most Linux systems. Other UNIX-like systems can default to other shells like csh or tcsh; there are many alternatives such as zsh that you may prefer. When you change shells the syntax of the following operations can change; however, conceptually all UNIX-like shells provide the same basic functionality:
- run external programs with command-line arguments
- view and set environment variables
- redirect program input and output using I/O redirection and pipes.
- allow for the creation of scripts that combine external programs with built-in programming functionality.
But at its most basic level, a shell allows you to type a command, and then the shell executes that command. Traditionally most commands are just the name of a program, the command just tells the shell to run the specified program, with the text after the program name passed as arguments to the program, much as you would pass arguments to a function. Command line arguments are separated by spaces from each other.
Here are some commands to get you started:
- ls: list the files in the current directory
- cd: change directory, e.g., cd /home. Note that "." refers to the current directory, and ".." refers to the parent directory. So "cd .." goes up one level.
- cat: output the contents of an input file to the specified file. This is useful for looking at smaller files.
- more and less: output the contents of larger text files, allowing you to navigate through them. (Guess which one came first?)
- vi and nano: Edit text files. (Which one is easier to use?)
If you are wondering which program binary a command is running, use which.
Many commands support the --help and --version options. Others will just give you information on the command if you run it with no options or with an option it doesn't understand. There are also manual pages that can be accessed with the man command, but that command isn't available in LinuxOnTab by default.
Processes
Each application running on a system is assigned a unique process identifier. The ps command shows the process identifiers for running processes. Each process running on the system is kept separated from other processes by the operating system.
You can also run the top command to get an interactive view of the processes running on the system. It also gives information about available system resources.
When you enter a command at a shell prompt, most of the time you are creating a new process which runs the program you specified.
Files, Links, and Filesystems
Files in Linux are combinations of two things: a file name (which is an entry in a directory) and file contents (which is represented by an inode). This separation allows for two kinds of links that associate a file name with file contents.
- A hard link binds a file name to an inode.
- A symbolic link binds a file name to another file name.
Note that both allow for multiple file names to refer to the same file contents, either through hard links to the same inode or through symbolic links all referring to the same filename.
A filesystem is a set of files that all come from the same source. This source could be some sort of persistent storage medium (e.g., an SSD), but it could be anything. When the system first starts it has the root filesystem in the / directory. Other filesystems are "mounted" onto specific directories, so all files are in a single hierarchy under /. Hard links are limited to a single filesystem, while symbolic links can span filesystems.
You can add, remove, and see what filesystems are mounted using the mount command. The df command can show how much free space is available on each mounted filesystem.
Environment & Shell Variables
Environment variables on both Linux and Windows are variable-value pairs that are shared between processes that define important context-related information (such as the name of the current user, the current language, the timezone) for applications. The key advantage of environment variables is that they are available right when a program starts - they are given to it by the operating system.
In Linux, these environment variables can be printed on the command line in most shells by referring to the variable name prefixed with a $ sign (eg: to output the value in the HELLO environment variable, one could write echo $HELLO).
Most shells also have internal variables which are private to the shell process. Typically you can access shell and environment variables using the same mechanisms. By convention, shell variables are lower case or mixed case, while environment variables are all upper case. In bash, by default all variables are first shell variables. To make them environment variables, they must be "export"-ed. Thus
X="Important Data"
just defines X for the current bash process. However, if you then type
export X
X will be turned into an environment variable, and so every subsequent program will also get X. You can combine both in one line:
export X="Important Data"
This is the idiom for setting environment variables normally.
To delete an environment variable, you can unset X.
One thing to remember with the above is that spaces are used to separate arguments in bash and most other UNIX shells. Thus it is an error to type:
export X = "Important Data"
as you now are giving export three arguments, not one.
One of the key reasons people choose alternatives to bash is because of quirks like this!
Controlling Processes
On Linux, you can control processes by sending them signals.
You send signals when you type certain key sequences in most shells: Control-C sends INT (interrupt), Control-Z sends STOP.
You can send a signal to a process using the kill command:
kill -<signal> <process ID>
So to stop process 4542, type
kill -STOP 4542
By default, kill sends the TERM signal.
Special Directories
On Linux, the filesystem is more than a system for storing programs and data. Some files are "special" and others have a zero size but contain arbitrary amounts of data. Three top-level directories are of particular importance: /dev, /proc, and /sys. We will be exploring these directories through the following questions.
Tasks/Questions
For the following questions, try to figure out the answers WITHOUT SEARCHING ONLINE. The point of these exercises is not the specific answers but the processes you use to find the answer. The information given above, plus some exploration, is enough to answer all of the following questions.
- Can you find any files where the size is zero but the file clearly contains data? What filesystem(s) contain such files?
- When you have logged in to a shell, how (i.e., using what commands?) do you first find out information about the environment?
- The version of your Linux distribution and the version of your Linux kernel.
- RAM, disk space, and CPU.
- Linux commands can be classified as internal (built into the shell) and external (separate program binaries). How can you tell if a specific command (e.g., cd) is internal or external? Figure out where at least three external commands reside on the system.
- By default df doesn't report on all mounted filesystems. Why do you think this is?
- What do you think busybox is? Where do you see it on the LinuxOnTab system?
- Run the following command. What is the value of $x afterwards? What if you interrupt it in the middle, what is the value of $x?
for x in 1 2 3 4 5 6 7 8 9 10; do echo $x; sleep 1; done
- What happens to the value of $x if you run the following?
unset x
- Run the following command. What is the value of $x afterwards? What if you interrupt it in the middle, what is the value of $x? How does this differ from the answer to Question 6?
(for x in 1 2 3 4 5 6 7 8 9 10; do echo $x; sleep 1; done)
- Run the following command. How does the behaviour of this command differ from previous commands? How can you stop the command from running? And what is the value of $x, at the end of the command and while it is running?
(for x in 1 2 3 4 5 6 7 8 9 10; do echo $x; sleep 1; done) &
- Can you make a command that outputs the numbers 1 through 10, delaying for a second in between, using the shell command language? Your program shouldn't be written on just one line! (Programs written in the shell language are known as shell scripts. Are there any other commands on the system that are actually shell scripts?)