The part of the operating system responsible for managing files and directories is called the file system. It organizes our data into files, which hold information, and directories (also called "folders"), which hold files or other directories.
Several commands are frequently used to create, inspect, rename, and delete files and directories. To start exploring them, let's open a shell window:
$
The dollar sign is a prompt, which shows us that the shell is waiting for input; your shell may show something more elaborate.
Type the command whoami
,
then press the Enter key (sometimes marked Return) to send the command to the shell.
The command's output is the ID of the current user,
i.e.,
it shows us who the shell thinks we are:
$ whoami
swc
More specifically, when we type whoami
the shell:
whoami
,Next,
let's find out where we are by running a command called pwd
(which stands for "print working directory").
At any moment,
our current working directory
is our current default directory,
i.e.,
the directory that the computer assumes we want to run commands in
unless we explicitly specify something else.
Here,
the computer's response is /users/swc
,
which my home directory:
$ pwd
/users/swc
Alphabet Soup
If the command to find out who we are is
whoami
, the command to find out where we are ought to be calledwhereami
, so why is itpwd
instead? The usual answer is that in the early 1970s, when Unix was first being developed, every keystroke counted: the devices of the day were slow, and backspacing on a teletype was so painful that cutting the number of keystrokes in order to cut the number of typing mistakes was actually a win for usability. The reality is that commands were added to Unix one by one, without any master plan, by people who were immersed in its jargon. The result is as inconsistent as the roolz uv Inglish speling, but we're stuck with it now.
To understand what a "home directory" is,
let's have a look at how the file system as a whole is organized.
At the top is the root directory
that holds everything else.
We refer to it using a slash character /
on its own;
this is the leading slash in /users/swc
.
Inside that directory are several other directories:
2014-12-16-lbl-student
(which we will be using)
bin
(which is where some built-in programs are stored),
data
(for miscellaneous data files),
users
(where users' personal directories are located),
tmp
(for temporary files that don't need to be stored long-term),
and so on:
We know that our current working directory /users/swc
is stored inside /users
because /users
is the first part of its name.
Similarly,
we know that /users
is stored inside the root directory /
because its name begins with /
.
Underneath /users
,
we find one directory for each user with an account on this machine.
The Mummy's files are stored in /users/imhotep
,
Wolfman's in /users/larry
,
and Vlad's in /users/vlad
.
Notice that there are two meanings for the
/
character. When it appears at the front of a file or directory name, it refers to the root directory. When it appears inside a name, it's just a separator.
Let's see what's in my home directory by running ls
,
which stands for "listing":
$ ls
2014-12-16-lbl-student bin data
mail music notes.txt
papers pizza.cfg solar
solar.pdf
ls
prints the names of the files and directories in the current directory in alphabetical order,
arranged neatly into columns.
We can make its output more comprehensible by using the flag -F
,
which tells ls
to add a trailing /
to the names of directories:
$ ls -F
2014-12-16-lbl-student/ bin/ data/
mail/ music/ notes.txt
papers/ pizza.cfg solar/
solar.pdf
Here,
we can see that /users/swc
contains sub-directories.
The names that don't have trailing slashes,
like notes.txt
, pizza.cfg
, and solar.pdf
,
are plain old files.
And note that there is a space between ls
and -F
:
without it,
the shell thinks we're trying to run a command called ls-F
,
which doesn't exist.
What's In A Name?
You may have noticed that all of my files' names are "something dot something". This is just a convention: we can call a file
mythesis
or almost anything else we want. However, most people use two-part names most of the time to help them (and their programs) tell different kinds of files apart. The second part of such a name is called the filename extension, and indicates what type of data the file holds:.txt
signals a plain text file,.cfg
is a configuration file full of parameters for some program or other, and so on.This is just a convention, albeit an important one. Files contain bytes: it's up to us and our programs to interpret those bytes according to the rules for PDF documents, images, and so on.
Naming a PNG image of a whale as
whale.mp3
doesn't somehow magically turn it into a recording of whalesong, though it might cause the operating system to try to open it with a music player when someone double-clicks it.
Let's go inside 2014-12-16-lbl-student
with cd
.
cd
stands for change directory.
$ cd 2014-12-16-lbl-student
cd
doesn't print anything.
Notice, by the way that we spelled the directory name.
It doesn't have a trailing slash:
that's added to directory names by ls
when we use the -F
flag to help us tell things apart.
And it doesn't begin with a slash because it's a relative path,
i.e., it tells ls
how to find something from where we are,
rather than from the root of the file system.
The leading /
tells the computer to follow the path from the root of the filesystem,
so it always refers to exactly one directory,
no matter where we are when we run the command.
Let's make sure we are in 2014-12-16-lbl-student
with pwd
.
$ pwd
/users/swc/2014-12-16-lbl-student
Let's look around with ls
.
$ ls
shell README.md
Now let's look a little bit closer let's run ls -a
.
$ ls -a -F
./ ../ shell/
README.md
-a
stands for "show all"; it forces ls to show us file and directory names that begin with .
, such as ..
(which, if we're in /users/swc/2014-12-16-lbl-student/
, refers to the /users/swc directory
). As you can see, it also displays another special directory that's just called .
, which means "the current working directory". It may seem redundant to have a name for it, but we'll see some uses for it soon.
Let's look at ls -F .
and ls -F ..
$ ls -F .
shell/ README.md
$ ls -F ..
2014-12-16-lbl-student/ bin/ data/
mail/ music/ notes.txt
papers/ pizza.cfg solar/
solar.pdf
We recognize the directory contents.
Let's go back to our home directory.
$ cd ..
$ pwd
/users/swc
Orthogonality
The special names
.
and..
don't belong tols
; they are interpreted the same way by every program. For example, if we are in/users/swc/data
, the commandls ..
will give us a listing of/users/swc
. When the meanings of the parts are the same no matter how they're combined, programmers say they are orthogonal: Orthogonal systems tend to be easier for people to learn because there are fewer special cases and exceptions to keep track of.
Let's take an excursion to the /bin
directory.
$ cd /bin
$ ls
Does anyone recognize any of the programs?
How do we get back to our home directory?
Knowing just this much about files and directories,
Nelle is ready to organize the files that the protein assay machine will create.
First,
she creates a directory called north-pacific-gyre
(to remind herself where the data came from).
Inside that,
she creates a directory called 2012-07-03
,
which is the date she started processing the samples.
She used to use names like conference-paper
and revised-results
,
but she found them hard to understand after a couple of years.
(The final straw was when she found herself creating
a directory called revised-revised-results-3
.)
Nelle names her directories "year-month-day", with leading zeroes for months and days, because the shell displays file and directory names in alphabetical order. If she used month names, December would come before July; if she didn't use leading zeroes, November ('11') would come before July ('7').
Each of her physical samples is labelled according to her lab's convention
with a unique ten-character ID,
such as "NENE01729A".
This is what she used in her collection log
to record the location, time, depth, and other characteristics of the sample,
so she decides to use it as part of each data file's name.
Since the assay machine's output is plain text,
she will call her files NENE01729A.txt
, NENE01812A.txt
, and so on.
All 1520 files will go into the same directory.
If she is in her home directory, Nelle can see what files she has using the command:
$ ls north-pacific-gyre/2012-07-03/
This is a lot to type, but she can let the shell do most of the work. If she types:
$ ls no
and then presses tab, the shell automatically completes the directory name for her:
$ ls north-pacific-gyre/
If she presses tab again,
Bash will add 2012-07-03/
to the command,
since it's the only possible completion.
Pressing tab again does nothing,
since there are 1520 possibilities;
pressing tab twice brings up a list of all the files,
and so on.
This is called tab completion,
and we will see it in many other tools as we go on.
/
on its own is the root directory of the whole filesystem.something.extension
.
The extension isn't required,
and doesn't guarantee anything,
but is normally used to indicate the type of data in the file.If pwd
displays /users/thing
, what will ls ../backup
display?
1. ../backup: No such file or directory
2. 2012-12-01 2013-01-08 2013-01-27
3. 2012-12-01/ 2013-01-08/ 2013-01-27/
4. original pnas_final pnas_sub
If pwd
displays /users/backup
,
and -r
tells ls
to display things in reverse order,
what command will display:
pnas-sub/ pnas-final/ original/
ls pwd
ls -r -F
ls -r -F /users/backup
What does the command cd
without a directory name do?
1. It has no effect.
2. It changes the working directory to /
.
3. It changes the working directory to the user's home directory.
4. It produces an error message.
What does the command ls
do when used with the -s and -h arguments?