1 <div class="objectives" markdown="1">
4 * Explain the similarities and differences between a file and a directory.
5 * Translate an absolute path into a relative path and vice versa.
6 * Construct absolute and relative paths that identify specific files and directories.
7 * Explain the steps in the shell's read-run-print cycle.
8 * Identify the actual command, flags, and filenames in a command-line call.
9 * Demonstrate the use of tab completion, and explain its advantages.
13 The part of the operating system responsible for managing files and directories
14 is called the [file system](../../gloss.html#filesystem).
15 It organizes our data into files,
16 which hold information,
17 and directories (also called "folders"),
18 which hold files or other directories.
20 Several commands are frequently used to create, inspect, rename, and delete files and directories.
21 To start exploring them,
22 let's open a shell window:
24 <div class="in" markdown="1">
30 The dollar sign is a [prompt](../../gloss.html#prompt),
31 which shows us that the shell is waiting for input;
32 your shell may show something more elaborate.
34 Type the command `whoami`,
35 then press the Enter key (sometimes marked Return) to send the command to the shell.
36 The command's output is the ID of the current user,
38 it shows us who the shell thinks we are:
40 <div class="in" markdown="1">
45 <div class="out" markdown="1">
51 More specifically, when we type `whoami` the shell:
53 1. finds a program called `whoami`,
55 3. displays that program's output, then
56 4. displays a new prompt to tell us that it's ready for more commands.
59 let's find out where we are by running a command called `pwd`
60 (which stands for "print working directory").
62 our [current working directory](../../gloss.html#current-working-directory)
63 is our current default directory,
65 the directory that the computer assumes we want to run commands in
66 unless we explicitly specify something else.
68 the computer's response is `/users/vlad`,
69 which is Vlad's [home directory](../../gloss.html#home-directory):
71 <div class="in" markdown="1">
76 <div class="out" markdown="1">
84 > If the command to find out who we are is `whoami`, the command to find
85 > out where we are ought to be called `whereami`, so why is it `pwd`
86 > instead? The usual answer is that in the early 1970s, when Unix was
87 > first being developed, every keystroke counted: the devices of the day
88 > were slow, and backspacing on a teletype was so painful that cutting the
89 > number of keystrokes in order to cut the number of typing mistakes was
90 > actually a win for usability. The reality is that commands were added to
91 > Unix one by one, without any master plan, by people who were immersed in
92 > its jargon. The result is as inconsistent as the roolz uv Inglish
93 > speling, but we're stuck with it now.
95 To understand what a "home directory" is,
96 let's have a look at how the file system as a whole is organized.
97 At the top is the [root directory](../../gloss.html#root-directory)
98 that holds everything else.
99 We refer to it using a slash character `/` on its own;
100 this is the leading slash in `/users/vlad`.
102 Inside that directory are several other directories:
103 `bin` (which is where some built-in programs are stored),
104 `data` (for miscellaneous data files),
105 `users` (where users' personal directories are located),
106 `tmp` (for temporary files that don't need to be stored long-term),
109 <img src="img/filesystem.svg" alt="The Filesystem" />
111 We know that our current working directory `/users/vlad` is stored inside `/users`
112 because `/users` is the first part of its name.
114 we know that `/users` is stored inside the root directory `/`
115 because its name begins with `/`.
118 we find one directory for each user with an account on this machine.
119 The Mummy's files are stored in `/users/imhotep`,
120 Wolfman's in `/users/larry`,
121 and ours in `/users/vlad`,
122 which is why `vlad` is the last part of the directory's name.
124 <img src="img/home-directories.svg" alt="Home Directories" />
126 > Notice that there are two meanings for the `/` character.
127 > When it appears at the front of a file or directory name,
128 > it refers to the root directory. When it appears *inside* a name,
129 > it's just a separator.
131 Let's see what's in Vlad's home directory by running `ls`,
132 which stands for "listing":
134 <div class="in" markdown="1">
139 <div class="out" markdown="1">
142 notes.txt papers pizza.cfg solar
147 <img src="img/vlad-homedir.svg" alt="Vlad's Home Directory" />
149 `ls` prints the names of the files and directories in the current directory in alphabetical order,
150 arranged neatly into columns.
151 We can make its output more comprehensible by using the [flag](../../gloss.html#command-line-flag) `-F`,
152 which tells `ls` to add a trailing `/` to the names of directories:
154 <div class="in" markdown="1">
159 <div class="out" markdown="1">
161 bin/ data/ mail/ music/
162 notes.txt papers/ pizza.cfg solar/
168 we can see that `/users/vlad` contains seven [sub-directories](../../gloss.html#sub-directory).
169 The names that don't have trailing slashes,
170 like `notes.txt`, `pizza.cfg`, and `solar.pdf`,
172 And note that there is a space between `ls` and `-F`:
174 the shell thinks we're trying to run a command called `ls-F`,
177 > #### What's In A Name?
179 > You may have noticed that all of Vlad's files' names are "something dot
180 > something". This is just a convention: we can call a file `mythesis` or
181 > almost anything else we want. However, most people use two-part names
182 > most of the time to help them (and their programs) tell different kinds
183 > of files apart. The second part of such a name is called the
184 > [filename extension](../../gloss.html#filename-extension), and indicates
185 > what type of data the file holds: `.txt` signals a plain text file, `.pdf`
186 > indicates a PDF document, `.cfg` is a configuration file full of parameters
187 > for some program or other, and so on.
189 > This is just a convention, albeit an important one. Files contain
190 > bytes: it's up to us and our programs to interpret those bytes
191 > according to the rules for PDF documents, images, and so on.
193 > Naming a PNG image of a whale as `whale.mp3` doesn't somehow
194 > magically turn it into a recording of whalesong, though it *might*
195 > cause the operating system to try to open it with a music player
196 > when someone double-clicks it.
198 Now let's take a look at what's in Vlad's `data` directory by running `ls -F data`,
200 the command `ls` with the parameters `-F` and `data`.
201 The second parameter—the one *without* a leading dash—tells `ls` that
202 we want a listing of something other than our current working directory:
204 <div class="in" markdown="1">
209 <div class="out" markdown="1">
211 amino-acids.txt elements/ morse.txt
212 pdb/ planets.txt sunspot.txt
216 The output shows us that there are four text files and two sub-sub-directories.
217 Organizing things hierarchically in this way helps us keep track of our work:
218 it's possible to put hundreds of files in our home directory,
219 just as it's possible to pile hundreds of printed papers on our desk,
220 but it's a self-defeating strategy.
222 Notice, by the way that we spelled the directory name `data`.
223 It doesn't have a trailing slash:
224 that's added to directory names by `ls` when we use the `-F` flag to help us tell things apart.
225 And it doesn't begin with a slash because it's a [relative path](../../gloss.html#relative-path),
226 i.e., it tells `ls` how to find something from where we are,
227 rather than from the root of the file system.
229 If we run `ls -F /data` (*with* a leading slash) we get a different answer,
230 because `/data` is an [absolute path](../../gloss.html#absolute-path):
232 <div class="in" markdown="1">
237 <div class="out" markdown="1">
239 access.log backup/ hardware.cfg
244 The leading `/` tells the computer to follow the path from the root of the filesystem,
245 so it always refers to exactly one directory,
246 no matter where we are when we run the command.
248 What if we want to change our current working directory?
250 `pwd` shows us that we're in `/users/vlad`,
251 and `ls` without any parameters shows us that directory's contents:
253 <div class="in" markdown="1">
258 <div class="out" markdown="1">
263 <div class="in" markdown="1">
268 <div class="out" markdown="1">
270 bin/ data/ mail/ music/
271 notes.txt papers/ pizza.cfg solar/
276 We can use `cd` followed by a directory name to change our working directory.
277 `cd` stands for "change directory",
278 which is a bit misleading:
279 the command doesn't change the directory,
280 it changes the shell's idea of what directory we are in.
282 <div class="in" markdown="1">
288 `cd` doesn't print anything,
289 but if we run `pwd` after it, we can see that we are now in `/users/vlad/data`.
290 If we run `ls` without parameters now,
291 it lists the contents of `/users/vlad/data`,
292 because that's where we now are:
294 <div class="in" markdown="1">
299 <div class="out" markdown="1">
304 <div class="in" markdown="1">
309 <div class="out" markdown="1">
311 amino-acids.txt elements/ morse.txt
312 pdb/ planets.txt sunspot.txt
316 We now know how to go down the directory tree:
318 We could use an absolute path:
320 <div class="in" markdown="1">
326 but it's almost always simpler to use `cd ..` to go up one level:
328 <div class="in" markdown="1">
333 <div class="out" markdown="1">
338 <div class="in" markdown="1">
344 `..` is a special directory name meaning
345 "the directory containing this one",
347 the [parent](../../gloss.html#parent-directory) of the current directory.
349 if we run `pwd` after running `cd ..`, we're back in `/users/vlad`:
351 <div class="in" markdown="1">
356 <div class="out" markdown="1">
362 The special directory `..` doesn't usually show up when we run `ls`.
363 If we want to display it, we can give `ls` the `-a` flag:
365 <div class="in" markdown="1">
370 <div class="out" markdown="1">
373 mail/ music/ notes.txt papers/
374 pizza.cfg solar/ solar.pdf swc/
378 `-a` stands for "show all";
379 it forces `ls` to show us file and directory names that begin with `.`,
380 such as `..` (which, if we're in `/users/vlad`, refers to the `/users` directory).
382 it also displays another special directory that's just called `.`,
383 which means "the current working directory".
384 It may seem redundant to have a name for it,
385 but we'll see some uses for it soon.
389 > The special names `.` and `..` don't belong to `ls`;
390 > they are interpreted the same way by every program.
392 > if we are in `/users/vlad/data`,
393 > the command `ls ..` will give us a listing of `/users/vlad`.
394 > When the meanings of the parts are the same no matter how they're combined,
395 > programmers say they are [orthogonal](../../gloss.html#orthogonal):
396 > Orthogonal systems tend to be easier for people to learn
397 > because there are fewer special cases and exceptions to keep track of.
399 #### Nelle's Pipeline: Organizing Files
401 Knowing just this much about files and directories,
402 Nelle is ready to organize the files that the protein assay machine will create.
404 she creates a directory called `north-pacific-gyre`
405 (to remind herself where the data came from).
407 she creates a directory called `2012-07-03`,
408 which is the date she started processing the samples.
409 She used to use names like `conference-paper` and `revised-results`,
410 but she found them hard to understand after a couple of years.
411 (The final straw was when she found herself creating
412 a directory called `revised-revised-results-3`.)
414 > Nelle names her directories "year-month-day",
415 > with leading zeroes for months and days,
416 > because the shell displays file and directory names in alphabetical order.
417 > If she used month names,
418 > December would come before July;
419 > if she didn't use leading zeroes,
420 > November ('11') would come before July ('7').
422 Each of her physical samples is labelled according to her lab's convention
423 with a unique ten-character ID,
424 such as "NENE01729A".
425 This is what she used in her collection log
426 to record the location, time, depth, and other characteristics of the sample,
427 so she decides to use it as part of each data file's name.
428 Since the assay machine's output is plain text,
429 she will call her files `NENE01729A.txt`, `NENE01812A.txt`, and so on.
430 All 1520 files will go into the same directory.
432 If she is in her home directory,
433 Nelle can see what files she has using the command:
435 <div class="in" markdown="1">
437 $ ls north-pacific-gyre/2012-07-03/
441 This is a lot to type,
442 but she can let the shell do most of the work.
445 <div class="in" markdown="1">
451 and then presses tab,
452 the shell automatically completes the directory name for her:
454 <div class="in" markdown="1">
456 $ ls north-pacific-gyre/
460 If she presses tab again,
461 Bash will add `2012-07-03/` to the command,
462 since it's the only possible completion.
463 Pressing tab again does nothing,
464 since there are 1520 possibilities;
465 pressing tab twice brings up a list of all the files,
467 This is called [tab completion](../../gloss.html#tab-completion),
468 and we will see it in many other tools as we go on.
470 <div class="keypoints" markdown="1">
473 * The file system is responsible for managing information on the disk.
474 * Information is stored in files, which are stored in directories (folders).
475 * Directories can also store other directories, which forms a directory tree.
476 * `/` on its own is the root directory of the whole filesystem.
477 * A relative path specifies a location starting from the current location.
478 * An absolute path specifies a location from the root of the filesystem.
479 * Directory names in a path are separated with '/' on Unix, but '\\' on Windows.
480 * '..' means "the directory above the current one";
481 '.' on its own means "the current directory".
482 * Most files' names are `something.extension`.
483 The extension isn't required,
484 and doesn't guarantee anything,
485 but is normally used to indicate the type of data in the file.
486 * Most commands take options (flags) which begin with a '-'.
490 <div class="challenges" markdown="1">
494 <img src="img/filesystem-challenge.svg" alt="Filesystem for Challenge Questions" />
496 1. If `pwd` displays `/users/thing`, what will `ls ../backup` display?
497 1. `../backup: No such file or directory`
498 2. `2012-12-01 2013-01-08 2013-01-27`
499 3. `2012-12-01/ 2013-01-08/ 2013-01-27/`
500 4. `original pnas_final pnas_sub`
502 2. If `pwd` displays `/users/backup`,
503 and `-r` tells `ls` to display things in reverse order,
504 what command will display:
507 pnas-sub/ pnas-final/ original/
512 3. `ls -r -F /users/backup`
513 4. Either \#2 or \#3 above, but not \#1.
515 3. What does the command `cd` without a directory name do?
517 2. It changes the working directory to `/`.
518 3. It changes the working directory to the user's home directory.
519 4. It produces an error message.