Hierarchical file system #
File name extension #
File names often end with an extension, which provides information about the type of the file.
For instance, the file name myDocument.pdf
has .pdf
as extension.
Examples. Common file name extensions include:
.mp3
for an MP3 file,.java
for a Java source file,.class
for a compiled Java file.
Warning. A file name extension is not mandatory, and has no incidence on the content of the file. For instance:
- a text file
myFile.txt
can be renamedmyFile.mp3
an still be opened with a text editor,- a bash script
myScript.sh
and the same file without the.sh
extension can be executed in the same way,- a “README” file is a text file (written in Markdown), often used to describe the content of a git repository. It can be named either
README.md
orREADME
.
However, appropriate file extensions may be needed for some programs to behave as expected.
For instance, an IDE or a code editor may use file extensions (like .java
) to identify the programming language at hand.
Tree #
Most operating system expose files as a (virtual) tree (or possibly several trees for Windows).
A (nonempty) tree is either:
- a single node (called a leaf), or
- a node with one or several children that are trees.
In this tree, non-leaf nodes are directories, and the leaves can be files, links, empty directories, etc. For instance:
├── bin
│ ├── echo
│ └── ...
├── home
│ ├── alice
│ │ ├── .bashrc
│ │ ├── .m2
│ │ │ └── ...
│ │ ├── Desktop
│ │ │ └── ...
│ │ ├── music
│ │ │ └── thatSong.mp3
│ │ └── workspace
│ │ └── pp
│ │ ├── assignments
│ │ │ └── ...
│ │ └── project
│ │ ├── .git
│ │ │ └── ...
│ │ ├── src
│ │ │ └── HelloWorld.java
│ │ └── test
│ │ └── testHelloWorld.java
│ └── bob
│ ├── .bashrc
│ ├── Desktop
│ │ └── ...
│ └── music
│ ├── thatSong.mp3
│ └── anotherSong.mp3
└── ...
Terminology. In this course, we will use the terms “folder” and “directory” interchangeably.
Terminology. “File” is sometimes used (e.g. in the Linux documentation) as a generic term for all nodes in this tree (in combination with “directory file”, “executable file”, “regular file”, etc.) We will not follow this terminology.
Windows may expose several trees (e.g. one per physical device).
Path #
Definition. In such a tree, a path from a directory $s$ to an arbitrary node $t$ is either:
.
if $s$ and $t$ are the same node, or..
if $t$ is the parent of $s$, or- the name of $t$ if $s$ is the parent of $t$, or
- a path from $s$ to some node $i$, followed by
/
(or\
on Windows), followed by a path from $i$ to $t$.
For instance (on macOS, Linux, etc.), in the tree above:
home/alice/workspace/pp/project/src/HelloWorld.java
is a path from the root to the fileHelloWorld.java
,workspace/pp/project/src/HelloWorld.java
is a path from the directoryalice
to the fileHelloWorld.java
,../../bob/Desktop
is a path from Alice’s Desktop to Bob’s Desktop,../../bob/../bob/./Desktop
is another path from Alice’s Desktop to Bob’s Desktop.
How many paths are there from a directory to a node?
(Countably) infinitely many.
Case sensitivity #
Warning. Some operating systems (Windows, macOS) use case-insensitive paths, whereas others (Linux, Android) use case-sensitive paths (this may also vary depending on physical storage devices).
For instance, on Windows,
src/HelloWorld.java
andsrc/helloworld.java
are the same path.In order to make sure that your code is portable:
- always use case-sensitive paths in your code (i.e. respect the names of your files, folders, etc.),
- do not create two files (or subfolders, etc.) in the same folder with identical names modulo upper/lower case (e.g.
Readme.md
andREADME.md
).
Path separator #
As mentioned above, Windows uses \
as path separator, as opposed to the /
used by other OSs (or in web URIs).
Warning (for Windows). Some programs executed on Windows automatically convert the
/
path separator into a\
(or the other way around).
Warning (for Windows). In Java, a path can be safely written with
/
. At run time (i.e. when the program is executed), this symbol will be interpreted based on the underlying OS (i.e. as\
on Windows, and as/
on other OSs). However, a path written with\
will be interpreted as such (therefore the program will throw an exception on OSs other than Windows).
Good practices #
Warning. The following are usually discouraged:
- a path separator (
/
or\
), a colon (:
), a whitespace character or a quotes/backtricks in a file or folder name,- a period (
.
) in a folder name.Your file browser and/or file system may let you to create such names. But these may cause some programs to fail.
Convention #
In what follows (unless explicitly stated), we will adopt by default the conventions for path names adopted on Linux.
In particular, we will use /
as a separator, and case-sensitive file and folder names.
Absolute path #
Definition. An absolute path is a path prefixed with:
/
on macOS, Linux, Android, IOs, etc.- a capital letter followed by
:\
on Windows, where the letter identifies a physical device, disk partition, etc. In particular, the identifier of the main partition is often the letterC
, in which case absolute paths (within this partition) are prefixed withC:\
.An absolute path is always a path from the root of the tree.
For instance (on macOS, Linux, etc.), /home/alice/workspace/pp/project/src/HelloWorld.java
is an absolute path to the file HelloWorld.java
A few simple observations:
- two different files may share the same name (but cannot share an absolute path), and
- two files with the same name may or may not be identical. E.g. in the above example, the two files named
thatSong.mp3
may or may not be identical.
Relative path #
Definition. A relative path is a path that is not absolute.
For instance, ../../bob/Desktop
is a path to Bob’s Desktop relative to Alice’s Desktop.
Note that if p is a relative path, then p prefixed with ./
(or .\
on Windows) is a path equivalent to p.
This prefix is sometimes used to emphasize that a path is relative (among other purposes).
Complete the table below, based on the tree above.
absolute path to source directory $s$ | absolute path to target $t$ | possible path from $s$ to $t$ |
---|---|---|
/home/alice/Desktop |
../../bob/music/anotherSong.mp3 |
|
/home/alice/music |
./../../../home/../home/alice/music |
|
/home/alice |
/home/alice/music/thatSong.mp3 |
|
/home/alice/music |
/home/bob/music/thatSong.mp3 |
|
project |
||
alice/../alice/.bashrc |
absolute path to source directory $s$ | absolute path to target $t$ | possible path from $s$ to $t$ |
---|---|---|
/home/alice/Desktop |
/home/bob/music/anotherSong.mp3 |
../../bob/music/anotherSong.mp3 |
/home/alice/music |
/home/alice/music |
./../../../home/../home/alice/music |
/home/alice |
/home/alice/music/thatSong.mp3 |
music/thatSong.mp3 |
/home/alice/music |
/home/bob/music/thatSong.mp3 |
../../bob/music/thatSong.mp3 |
/home/alice/workspace/pp |
/home/alice/workspace/pp/project |
project |
/home |
/home/alice/.bashrc |
alice/../alice/.bashrc |
Working directory #
Each process (e.g. a process that executes a Java program) has a working directory, which is a directory of the hierarchical file system. This directory may vary depending on the program and/or how it was started.
Many programs (implicitly) interpret a relative path as relative to their working directory.
In particular, this is the case of:
- Java programs,
- file browsers,
- shells.
Home directory #
Each user of a system has its own home directory. E.g. in this example:
/home/alice/
for Alice/home/bob/
for Bob
Warning. The home directory is usually not the root of the tree (even if there is a single user on the machine).
Absolute paths to the home directory #
The home directory (of a regular user) is normally:
/home/<userId>
on Linux/BSD,/Users/<userId>
on macOS,/User/<userId>
on iOS,/data/media/<userId>
on Android,<mainHardDriveId>:\Users\<userId>
on Windows (e.g.C:\Users\<userId>
).
On Linux and macOS, the alias ~
can be used as an absolute path to the current user’s home directory.
For instance, in the above example, if Alice is logged in, then ~/music/thatSong.mp3
and /home/alice/music/thatSong.mp3
are two absolute paths to the same file.
Access restriction #
Files and programs within the home directory of a user can usually be accessed by this user only (or a system admin).
Therefore programs that can be executed by all users are usually located in other directories (e.g. the program /bin/echo
in the example above).
Access restrictions (read, write and/or execute) may apply to the content of such directories. Notably, on Linux systems, (most of) the content of these directories can only be modified by a system admin.
Hidden files and directories #
The home directory often contains files (resp. directories) whose names are preceded with a .
(e.g. the file /home/alice/.bashrc
in the above example).
These are sometimes called “dot files” (resp. “dot folders”).
On Linux and macOS, dot files and dot folder are hidden by default in a terminal or a file browser.
On Windows, a (dot or not) file or folder can also be hidden, via a dedicated “hidden” attribute.
Hidden files and directories may be used to store (user-specific) parameters or options for a program.
For instance, the configuration file
~/.gitconfig
can be used to declare parameters (e.g. user identifier, etc.) or options (e.g. disable default fast-forward merge, etc.) to be used with git.
Hint. Configuration files are a convenient way to save your personal preferences and use them on several machines. For instance, you may need to connect to a remote server via ssh to perform some tasks (e.g. deploy a database or run costly computations). In such a scenario, you can upload dot files to your home directory on this server.
Hidden files or directories may serve other purposes.
For instance, the dot folder
~/.m2
is used by Maven to store (in one place) the Java libraries that are required (as dependencies) by any Java project present in the home directory.
Hidden files or directories may also contain information specific to a subdirectory.
For instance, every git repository (a.k.a. “project”) contains a dot folder .git
that stores the full history of modification made to the repository.
Usage and layout #
Your home directory is where you generally want to store your projects, documents, scripts, etc.
You are free to structure it as you like (using sub-directories). Make sure that this structure allows you to retrieve your own work easily.
Warning. When you download or create a project/document in your home directory, choose its location.
- Do not let an application decide this location for you.
- Do not rely exclusively on the “recently opened” feature of an app to locate your files.
- Do not store files and project directly at the root of your home directory.
Warning. Aside from a few exceptional cases, your home directory should not contain multiple copies of a project or document.
Hint. It can be difficult for a new developer to figure out when to accept the default directory or layout suggested by a program, and when to overwrite it. As a (soft) rule of thumb, you generally want to choose the location of:
- a file downloaded from your browser (or mail client),
- a project created with your IDE,
- a cloned git repository,
- a file created with an app,
- etc.
Conversely, it is generally recommended to follow suggestions in the following cases:
- internal structure of a project (typically the one generated by an IDE): e.g. the standard directory layout for a Maven project, or the standard directory structure of a Node JS project.
- software installed outside of your home directory,
- location of dot files and folders,
- etc.