The File System¶
Here are the commands you will learn in this module.
||List the contents of the current directory|
||Print the working directory|
||Change the working directory|
||Write something to the screen|
||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.
|1||Prompt||Print the prompt and wait for the user to type a command and the
|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
|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
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.
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:
||List the contents of the current directory|
||List a single file or directory|
||Long listing (one per line) with extra information|
||List all files (including hidden files)|
||Show the directory tree|
Here’s a demonstration of
ls command uses a switch. Try running both commands on your own:
$ ls $ ls -l
-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.
tree command and look at the output. You should see output very similar to mine.
The Current Directory¶
There’s no place like home!
cdwith 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.
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 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 /home/cis90/simben90
Here’s the absolute path of my
$ 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.