The File System & The Terminal
The draft of what might one day become a fujoweb.dev article
Warning: Almost There but Not Quite
The post you're about to read is marked as pre-beta! While the content should be overall accurate and understandable, it was not reviewed for flow, imprecisions, typos, or accidentally-abandoned sentences.
Venturing through? Let me know your thoughts!
Table of Contents
What’s a File System, anyway?
Think of data stored on a disk as a primordial soup of 0
s and 1
s. While all
the information you have saved does exist somewhere on the disk, it cannot be
retrieved without something to give it boundaries and “shape”. The file system
is the part of the operating system in charge of reading and writing “files” out
of this sea of data1.
What File Systems Do
Aside from knowing how to retrieve the content of a file from the disk it’s saved on, file systems also maintain additional information about each file. For example, a file system might keep track of, among many other things, when a file was created, when it was last accessed, and which user owns the file.
However, the most popular function of the file system is to maintain a
human-readable way to organize–one might say, file away–this data. It
does this is by keeping track of the path
of each file within a
system of directories
(also called folders
).
Folders and directories
When file systems were first created, the files in a system would be presented as a single, long list. However, when this proved unwieldy to manage, people decided they needed a way to group related files together. While this might seem obvious in retrospect, it wasn’t immediately so at the time: to figure out the right organization system, people turned to the real world for guidance.
In particular, two metaphors surfaced: folders
and directories
2. While
folder
is the more popular metaphor for laypeople, directory
is the preferred
term when talking about programming.
The folder metaphor
Imagine each file as a sheet of paper. To keep related files together, you could
place them in a paper folder
to collect them. Then, you might imagine yourself
continuing to group related file
s and folder
s together, placing them within
their own folder
, then within another folder
, then maybe within yet another
folder
… eventually, thanks to your zelous organizing, you end up with
a single folder
that contains all the file
s and folder
s in your system.
The directory metaphor
Similarly, imagine you’re searching for a file somewhere in your system. To
find the data seek, you might consult a directory
that lists every file along
with its “address” in the system. As the amount of files grows big, you may
want to have a multi-level system, where each level has its own directory
to
point you towards either the files at that level or the nearby directories where to
continue your search.
The File System Tree
Regardless of which metaphor you like best, the end result is a branching structure with every file in your system placed somewhere within it. The shape of this structure is one of the most common and studied in programming: a tree3.
Finding your Path
One of the advantages of this structure is that each file occupies a unique
position within it. To point someone to a specific file, you can start from
the top-most layer and lay out to the specific sequence of directories
they must
move through to find it. The path
of a file is the sequence of directories
one must follow to reach a file, starting from the top of the file system.
Unix vs Windows
While the general metaphor is the same, there are some differences on how Windows and Unix4 systems represent paths that’s important to be aware of. These are:
- Top-level folder/directory: in Windows, the top-level folder is prefixed
with a single capital letter indicating the disk it belongs to. For example,
you likely have seen
C://
as the top level folder of theC
disk. In Unix, the top level folder is always/
. - Path separator: In Windows, each
directory
in a path is historically separated with a backward slash (“\
”). Unix systems always use the forward slash (“/
”).
Useful file system terms and concepts
When talking about file systems, a few terms appear over and over:
- Root: The top-level
directory
, where it all starts from. This is usually represented with/
. - Parent: Each
directory
(aside from theroot
) has anotherdirectory
that contains it: its parent. Computer scientists usually refer to the elements under a parent as its children, though is not as common in file systems. - Subdirectory/Subfolder: If you put a
directory
within adirectory
, that’s called asubdirectory
of the parentdirectory
. You can use a similar terminology forfolder
. - Nesting: When a
directory
is contained within anotherdirectory
, we say it’s “nested” within it.
Your own personal space: Home Sweet Home (~
)
In modern operating systems, each user is assigned a “home directory”, their own special folder where to store their data. The home directory of a user is usually inaccessible to other users, making it a safe place to collect personal files.
The symbol for the home directory is ~
. You’ll see it around a lot.
On Windows, the home is generally under C:\Users\[username]
. On Unix
systems, the position changes quite a bit, but will generally be some variation of
/usr/[username]
.
Sneaking around: hidden files and folders
To make things more tidy, or to hide them from less experienced eyes, files and folders can
be marked as hidden. In Unix systems, this is done by prefixing their name with a “.
”.
It is often used for configuration files (like .env
) or program folders that should not be
manually edited by most users (like .git/
).
Moving across the file system, Terminal Edition
One of the most important things to do to get comfortable using a terminal is to learn to travel across the filesystem. You only need to learn three simple commands to make moving around a breeze:
- “Where am I?” (
pwd
) - “What’s here?” (
ls
) - “Let’s go somewhere else” (
cd
)
While Windows has its own unique commands, it has now adapted to the Unix style. If you’re using PowerShell as your terminal (you should), then many Unix commands will be available to you5.
Oh, user, where art thou? (pwd
)
The first, most important question to learn how to answer is: where am I? To
do this, you can use the pwd
command. pwd
stands for Print
Working Directory, but if you need another mnemonic I propose
“Pardon, Which Directory (is this)?”
Running pwd
will show you the path
you’re currently at.
Show Me Your Secrets Files! (ls
)
You can look around your current position with the ls
command. ls
stands for
list, but if you need another mnemonic, I suggest “Let’s See”.
One caveat about ls
is that it doesn’t show hidden files or directories (the ones
starting with .
) by default. While this keeps things tidy, you might want to
see everything your current position has to offer. You can list hidden files and directories
by using the -a
flag (a
stands for “all”).6
You can use a path with ls
to see what’s in a directory different from the one you’re
currently in.
Beam me anywhere, Scotty (cd
)
To go to a different place, you can use the cd
command. cd
stands for Change Directory, and if you need a
different mnemonic you’re out of luck cause I really can’t do better than that.
You use cd
by following it with the path
you want to move to. For example, to move to your home folder, you could use:
cd
can use both relative and absolute paths. But since you still don’t know what this means,
let’s go learn that.
Quick tip: you can use
cd -
to go back to where you were before your last directory change.
Absolute vs Relative Paths
There are two ways to point to a location in a file system:
- In an absolute way that describes the
path
to take from the root of the file system. - In a relative way that describes the
path
to take from a different position within the system, most often the one you’re currently at.
Working with absolute paths
Absolute paths always start from the root of the file system. This means that absolute path
always start with /
or with ~
(which is a shorthand for /usr/[username]
).
Running pwd
in a directory will show you its absolute path.
Working with relative paths
Relative paths start from where you currently are in the file system. Paths
are interpreted as relative when they don’t start with /
or ~
.
There are two special symbols to remember when working with relative path:
.
(single dot) indicates your current directory.directory_name/
(without prefix) is equivalent to./directory_name/
, that is the “directory_name/
” directory under your current directory “./
”...
(double dot) indicates the parent directory. This shorthand helps you visit your whole family tree. For example,../../
will be your grandparent directory, while../another_directory/
will allow you to point to your “sibling directory”.
Final cheatsheet
Here’s a final cheatsheet to help you remember what we learned.
While there’s an infinite amount of things to say about file systems, this is an overview of what you need to know to navigate them in everyday programming.
Footnotes
-
While there’s a lot of different types of file systems (you might have heard of
FAT
orNTFS
), their differences don’t generally matter to most users, even programmers. ↩ -
Theoretically there’s a difference, but practically there’s not. Don’t even worry about it. ↩
-
Two computer scientists sitting in a tree, T-R-A-V-E-R-S-I-N-G ↩
-
MacOS + Linux ↩
-
The ability to use these commands on a terminal is required for
POSIX
(Portable Operating System Interface) compliance. Using a terminal compliant withPOSIX
will allow you to use the same commands across any system. ↩ -
Flags are options added to a terminal command to change their behavior. They can be single letters preceded by a
-
(like-a
), or words preceded by a--
(like--all
). Often there are long form synonyms for single letters shorthands, but not always. ↩