CSE 374, Lecture 1: Linux and the shell

File systems

Let's start at the beginning. The data on your computer is stored in what we call a "file system". A file system is composed of "directories" or "folders", each of which contain other directories or individual "files", which contain data. This file system is actually a tree.

                          / (root)
                             |
               -----------------------------
              |              |              |
         Applications      System         Users
              |              |              |
             ...            ...     ---------------
                                   |               |
                                 mwinst          alice
                                   |               |
                             ------------         ...
                            |            |
                        Documents    Downloads
                            |            |
                        ---------       ...
                       |         |
                      foo       bar.txt

A "path" is a route from the root to a file or directory.

We can refer to things in the filesystem either by their "absolute path" or their "relative path".

What you're used to

Typically, when we're using a computer, we use a graphical user interface or "GUI" to accomplish tasks. For instance, when I start up my computer, I see a text box that I click on and enter my password, then see a "desktop" that displays many buttons for different applications. If I want to view a text file on my Mac, I click on a button that launches an application called Finder. Finder displays a list of all the folders and files on my computer, and I can click through to change folders and double click to open a file in another application called TextEdit. I can then modify the file and save it by clicking on the menu bar and then on the "Save" option. I can copy a file by right clicking on its name and selecting the "copy" option, or I can click, hold, and drag the file to move it someplace else. I can delete the file by right clicking on it, or by clicking and dragging it to the "trash can".

A textual alternative

In this class, we're going to translate these operations from a graphical user interface to a textual user interface. Instead of clicking on things, we'll use the keyboard to get around.

Let's try it. I want to log in to the "klaatu" Linux computer that is provided by the CSE department. In order to do this, we use an application called a "terminal". To get to text-world, we open up the terminal (in Windows, this would be Putty; in Mac or Linux you have several options).

"Click on my photo, enter my password, and click the arrow" becomes

    $ ssh mwinst@klaatu.cs.washington.edu

and then I enter my password and hit enter. Here "mwinst" is my username and "klaatu.cs.washington.edu" is the name of the computer that I am logging into. "ssh" is a program that takes the text that I type on my local computer and transfers it to the remote computer (which is in the Computer Science building).

Now where are we? What do we see? We are running a program called a "shell" on the klaatu machine. The "shell" takes text that you type ("commands") and executes the programs that correspond with those commands. All of the "commands" that we are about to talk about are actually little programs that do things.

"Click on Finder button to display files" becomes

    $ pwd
    /homes/mwinst
    $ ls
    foo bar.txt

Here, "pwd" stands for "present working directory", and it launches a computer program that displays the path to the current directory, where you are. We are in a folder called "homes" and a subfolder called "mwinst" (which is what we call my "home directory", just like your user's directory in Mac or Windows). "ls" stands for "list segments", and it lists all the files and folders within the present working directory. In this case, there is a folder called "foo" and a file called "bar.txt" in the current directory.

You can also provide arguments to a shell commend. You can add a folder after "ls" if you want to see the files inside a sub-folder. If there is a folder named "foo" in the present working directory, you can do

    $ ls foo
    bar baz

"Double clicking on a folder to view the files in that folder" becomes

    $ cd foo
    $ pwd
    /homes/mwinst/foo

Here, "cd" stands for "change directory", and we move from the current directory to a child directory called "foo". Note that here we've used the "relative path" for the child directory foo - since we are currently in the directory /homes/mwinst, we can provide the name of "foo" relative to that directory, which is just "foo". We could also have done this same thing with the "absolute path" of foo.

    $ cd /homes/mwinst/foo

How could I get back to the original directory? By providing the name of that directory.

    $ cd /homes/mwinst

"View the contents of a file" becomes

    $ cat test.txt
    This is an example text file.

"cat" is a command that displays the stuff that is stored in a file.

"Copy a file" becomes

    $ cp test.txt test2.txt
    $ cat test2.txt
    This is an example text file.

"cp" stands for "copy" and it copies the first file (test.txt) and names the copy the second file name provided (test2.txt). Now if we "cat" the new file, we see that it contains exactly the same text as the original file.

"Drag and drop a file to move it" becomes

    $ mv test2.txt test3.txt
    $ cat test2.txt
    cat: test2.txt: No such file or directory
    $ cat test3.txt
    This is an example text file.

"mv" stands for "move", and it moves the file to the new name and location. It removes the old file, as we can see when we try to "cat" it (it no longer exists).

"Remove a file by right-clicking and selecting delete" becomes

    $ rm test3.txt
    $ cat test3.txt
    cat: test3.txt: No such file or directory

"rm" stands for "remove", and it deletes the file. It's important to note that for both mv and rm, there is NO undo. You can't undo this by dragging a file from the Trash back to the desktop - so be careful what you're doing.

"Logging out" becomes

    $ exit

Why text?

OK, so why would we actually want this? Isn't a GUI a much better option?

Most computer scientists use a combination of GUIs and shells, depending what they’re doing.

Getting help

So far we've seen a bunch of simple commands that help us navigate and examine the file system (ls, cd, cat, cp, mv, rm). But now I want to do something more complicated: I want to list not just the contents of the current directory (which I could do with "ls") but also the time at which each file was last modified. This was something easy to see in the GUI world: the Finder showed times next to the name of each file. How will we figure out how to do this?

Linux has a built-in manuals (files containing documentation) that describe the commands and programs that we've been running. We call these "man pages", and we can access them by using the "man" command.

    $ man ls

That command gives us a large amount of information about how the "ls" command works. We can use the up and down keys to view the whole page (or, as the man page directs at the bottom of the screen, press "h" to learn more about how to navigate a man page). The man page lists how to use the ls command: provide the ls command followed by one or more "options" and then the name of a file.

    ls [OPTION]... [FILE]...

What is an option? The man page lists those too. Options are usually represented as one or two dashes followed by one or more characters, and they customize the behavior of the command. For example, in the ls man page we can see that the "-c" command, when used with the "-lt" command, will "sort by, and show, ctime (time of last modification of file status information)". So to achieve our goal of displaying files with modification time, we can run the following command:

    $ ls -c -lt

Options can be combined behind a single dash. The previous command is identical to this one:

    $ ls -ltc

You can use the "man" command to find out information about other commands, too:

    $ man mv
    $ man rm
    $ man pwd

Man pages are viewable online. Check out the Links section of the course webpage to find a link to the online Linux man pages reference. You can explore the documentation to learn about other commands (check out the intro link). You can also use the Linux pocket guide, if you've bought it, to learn about different commands and tools.

Another way to get help and understand how a command works is to use the --help option:

    $ ls --help

Most built-in commands and also most more complicated programs that you run from the command line support the --help option. The output from --help is printed to the terminal, not displayed in the nice format of the man page, but it is another option for you.

What if you forget all the things that we've discussed today? Well the shell will help you out if you just type "help".

    $ help

Summary