The File System

Here are the commands you will learn in this module.

Command Action
ls List the contents of the current directory
pwd Print the working directory
cd Change the working directory
echo Write something to the screen
banner Make a big banner (for fun)

How Commands are Processed

Commands are executed in stages. Understanding the stages is important to understanding error messages that appear when you make a mistake typing a command.

Step Title Description
1 Prompt Print the prompt and wait for the user to type a command and the Enter key
2 Parse Parsing is the process of turing the command you typed to a plan for action. During the parsing phase the shell breaks the line into a command and arguments. The shell also processes special characters like the wildcard * character and shell variables.
3 Search Once the command name is determined the shell searches for the command.
4 Execute When the command is found the shell exectues the command.
5 Nap While the command is running the shell goes to sleep and waits for the command to finish.
6 Repeat Go back to step 1!

The most common mistakes are making a typo in a command or trying to execute a command that doesn’t exist. Here’s what the shell tells you when the search for a command fails:

simben90@opus:~$ bogus 
bogus: command not found

Remember the words command not found. They tell you that there’s a problem with the command you entered. The message comes from step 3 when the search fails.

simben90@opus:~$ cat nosuchfile
cat: nosuchfile: No such file or directory

In the example above cat failed to find the file. Here’s another example of an error from cat:

simben90@opus:~$ cat -x 
cat: invalid option -- 'x'
Try 'cat --help' for more information.

The error messages from the last two examples come from cat itself during the step 4, Execution. When an error comes from a command the error message depends on the authors of the command. There are too many errors to list or know.

echo and banner: The shell talks back

The echo and banner commands repeat their arguments. The echo command is useful when you want a shell program to print something (we’ll talk about shell programming near the end of the semester). The banner program turns its arguments into a big-size banner.

Forms of the command:

Command Form Description
echo [message...] Repeat the words on the command line.
banner [message...] Make a banner using the words on the command line.

Here’s a demo:

Files and Directories

Files are the way that computers store data permanently. On graphical desktops files are organized into folders, on Unix the proper term is directory. The ls command lists the contents of the current directory. I’ll explain the current directory later.

You should know these forms of the command:

Command Form Description
ls -l List the contents of the current directory
ls <file|directory> List a single file or directory
ls -l Long listing (one per line) with extra information
ls -a List all files (including hidden files)
tree Show the directory tree

Here’s a demonstration of ls.

Notice the ls command uses a switch. Try running both commands on your own:

$ ls 
$ ls -l 

The -l switch means long listing. In long form the ls command prints one file per line with a bunch of extra information that we’ll talk about in the next lesson. The ls command color codes its output. Directories are colored blue in the example above.

Directories contain both files and other directories. The tree commands shows the structure of the current directory.

Run the tree command and look at the output. You should see output very similar to mine.

The Current Directory

There’s no place like home!
Running cd with no arguments takes you to your home directory.

When you use the file tool on your operating system the graphics on the screen show you files and folders at the same time and you pick what you want. On the command line you navigate folders by “walking” from one directory to another. The key concept that you have to remember is that of the working directory. That’s the folder you’re working in. Think of it like the place you’re standing.

To help you understand the place you are standing let’s play a game from my childhood: Beyond Zork: The Coconut of Quendor

In the game you walk from place to place. The graphic on the screen helps you see the effect of moving in different directions. When you use the command line the cd command moves you from place to place, the ls command shows you what’s around and the pwd command shows you where you are.

Arguments and File Names

Many commands take arguments that contains the name of a file or a directory. So how you do you tell a command like cat where to find a file? In the previous example we named a file in the current directory and cat showed us the contents. Try this starting in your home directory.

$ cd
$ cd Poems 
$ cd Angelou
$ cat bird 

But what if we want to print the bird file from another place? There are two options.

Relative Paths

A relative path is tells a command how to find a file starting from the current directory.

$ cd
$ cat Poems/Angelou/bird 

In the example you start in the in your home directory. The relative path to a file changes when you change directories. Here are a few different ways to print the contents of the bird poem. Try running these commands:

$ cd 
$ cat Poems/Angelou/bird 
$ cd Poems 
$ cat Angelou/bird 
$ cd Angelou
$ cat bird

The special directory .. refers to the directory above the current directory. Start from the Angelou directory and try the following commands:

$ ls .. 
$ ls ../..
$ ls ../../..

Absolute Paths

Absolute paths are not relative to the current working directory. Absolute paths begin with the / character which signifies the root directory. The pwd command always shows you an absolute path:

$ pwd

Here’s the absolute path of my bird poem:

$ cat /home/cis90/simben90/Poems/Angelou/bird 

The nice thing about an absolute path is that it works no matter where you are. The down side is that they are generally longer than a relative path.

Viewing Files