The filesystem and paths¶
Files¶
On a unix system, a regular file is a sequence of bytes, plain and simple. As much as possible, those bytes represent plain text (ASCII originally, now more commonly the backwards-compatible UTF-8), but there is nothing in the filesystem that knows about file types, extensions, binary-versus-text, or any other distinctions maintained by some filesystems. There is some metadata associated with each file to keep track of which user on the system owns it, when it was created and modified, who is allowed to edit it, and so forth, but a file doesn’t even know its own name or where it fits in; that information is maintained in directories.
Directories¶
Files are organized using directories. The image is of something like a telephone directory; a filesystem directory maps names to numbers, where the numbers are internally meaningful to the filesystem and show where to look up a particular entry. The entry can be a file, but it could also be another directory that lets you find yet more files, and so on. Since the directories and files are normally organized into a tree, the ‘directory’ metaphor is often interchangeable with the ‘folder’ metaphor. Whether you think about directories of named links that can take you places, or folders containing nested folders, it usually won’t matter, but in some more advanced reasoning about the unix filesystem sometimes it will be necessary to think of links.
There are other kinds of entries than just files and directories, but we won’t be getting deep into that. For example, there are special file-like objects that represent devices present on the system, such as removeable drives, but to use the files in such a device, it gets attached (‘mounted’) to the main directory tree so that paths never have to specify what device is providing any given file.
Paths¶
A path specifies a filesystem entry by how to find it in a sequence
of directories. The top of the filesystem hierarchy is called the
root directory, as in the root of the tree, and its name is /
(‘slash’). The entries in the root directory lead to various system
directories such as the ones where programs are installed, configuration
files are kept, and where users keep their personal files.
Steps in a path are separated by /
characters as well, so an
example path might be /home/rsurton/hello/hello.c
, which means
a particular file found in the following way.
Start at the root directory (
/
)Follow the link named
home
(to the collection of users’ home directories)From there, follow the link named
rsurton
(to the home directory of the user namedrsurton
)From there, follow the link named
hello
(a directory I have in my home directory for collecting files for a project I’m working on named ‘hello’)From there, follow the link named
hello.c
(a C source code file in that project)
Every file in the filesystem can be described by a path like that, starting at the root directory, which is called an ‘absolute path’.
Every running program also has a directory where it is currently
working, called its ‘working directory’. As you issue commands,
for example, you are always doing it from somewhere; there is a
directory you are ‘in’. If I am currently in my own home directory
(/home/rsurton
), I don’t need to call my project directory
/home/rsurton/hello
, I can just call it hello
,
and the C file I used as an example above could just be called
hello/hello.c
. Paths like that don’t start with a slash, but if
there are multiple steps the slash separator is still used between each
step and the next. Such a path is called a ‘relative path’, i.e.
relative to the current working directory.
Special names¶
Every directory, in addition to whatever else has been put there, always
has two particular entries. One, simply named .
(‘dot’),
is a link from the directory to itself, and the other, named ..
(‘dot-dot’), is a link to the directory in the hierarchy that contains
it, i.e. its parent directory. So, for example, ./hello
is a long
way of saying the same relative path as hello
(think of it as
saying ‘start here and then follow the link hello
’, rather
than just ‘follow the link hello
’). For another example, the
path /home/rsurton/hello/..
is the same as /home/rsurton
(i.e. ‘…follow the link rsurton
, then follow the link
hello
, then go back up one step’).
Most unix tools treat files whose names start with a period as special,
ignoring them unless they are specifically mentioned. This makes them, by
convention at least, hidden files. So, you won’t normally see .
or ..
, and you can make other files hidden as well simply by
naming them starting with a period like .name
.
Another name that is special is ~
(‘tilde’). When you are
typing commands, this symbol is expanded to refer to your own home
directory. Your home directory (usually the directory in /home
that is named the same as your username) is your default working directory
when you sign in, and it is your own little corner of the filesystem
where you keep your personal files. You so often want to go there, talk
about files there, and construct paths from there that the shell offers
this one-character abbreviation for home.