The File System & The Terminal

The draft of what might one day become a fujoweb.dev article

Published on February 11, 2024

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 0s and 1s. 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.

A sketch of Boba-tan looking across a landscape with a large icon for the
program Word on the horizon, next to a shadowed figure labeled “A OS character
(which we don’t have)”. At the bottom corner is the icon for the Finder program
for MacOS. It’s set up to be a reference to an Evangelion screenshot with two
people looking across a landscape with a large face in the sky.

(End of Evangelion voice) “I knoooowww, I know I’ve let you dowwnnn~”

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 awaythis 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 directories2. While folder is the more popular metaphor for laypeople, directory is the preferred term when talking about programming.

The folder metaphor

A sketch of Boba-tan carrying a huge stack of papers tied with a bow.

Filing away her fanfic collection.

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 files and folders 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 files and folders in your system.

The directory metaphor

A sketch of Boba-tan looking at a filesystem. Text:
Welcome to ~/documents
At this level: doujin.zip, draft_final_f_, fasdkjf.png, Screen Shot 3…
Nearby levels: .., art/, asdfsfa/, fics/, memes/, not-porn/, programming/, scans/, untitled/, untitled(1)/, untitled(2)/, websites/

“This filesystem is a maze!”

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.

A doodle of a tree of folders. At the top is one named with just a slash mark.
It branches out to “apps” on one side, then on the other side “tmp” with “logs”
under it, then another branch goes “users” - “boba-tan” - “art”. Under
“boba-tan” is also “code”.

If you rotate the picture 180 degrees, you can imagine the top level folder as the base of a tree (its “root”), while the branching paths form its, well, “branches” (that end in “leaves”). Yes, these are all technical terms.

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:

Useful file system terms and concepts

When talking about file systems, a few terms appear over and over:

A doodle of some nested folders. Under a folder marked only with a slash symbol
is “down”, then under that is “the”, then under that is “path”. The last folder
has a subtitle of “/down/the/path/“.

Do not talk to me or my subdirectory, or my subdirectory’s subdirectory, or my subdirectory’s subdirectory’s subdirectory, or my subdirectory’s subdirectory’s subdirectory’s subdirectory ever again.

Your own personal space: Home Sweet Home (~)

A sketch of Terminal looking at a cross-stitch that says “[tilde] sweet [tilde]”.

He cross-stitched that himself.

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:

  1. “Where am I?” (pwd)
  2. “What’s here?” (ls)
  3. “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.

Terminal window
pwd
# output
/users/boba-tan/programming/blorbo-shrine/

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”.

Terminal window
ls
# output
art/
asdfkasdfhjk.png
doujin.zip
memes/

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

Terminal window
ls -a
# output
.surprise-file
.surprise-directory/
art/
asdfkasdfhjk.png
doujin.zip
memes/

You can use a path with ls to see what’s in a directory different from the one you’re currently in.

Terminal window
ls -a ~
# output
.ssh/
.zsh/
art/
asdfasdfasdf.png
programming/

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:

Terminal window
cd ~
# equivalent to:
cd /usr/boba-tan

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:

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]).

Terminal window
# go to the fanfics folder in the user home
cd ~/fanfics
# list all the users folders
ls /usr/

Running pwd in a directory will show you its absolute path.

Terminal window
pwd
/usr/boba-tan/programming/

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:

Terminal window
# go visit your parent
cd ..
# see what your grandparent has to offer
ls ../..
# stay exactly where you are!!
cd .
# now show me your secrets
ls -a .

Final cheatsheet

Here’s a final cheatsheet to help you remember what we learned.

A doodle of a tree of folders. At the top is one named with just a slash mark.
A cat is sleeping on top, marked as "git sleeping". The subtitle of the folder is 
"the root".

It branches out to two unnamed folders on each side and one below marked users/. 
On the folder on the right side is terminal, fighting with a cat marked as 
"github hissing". GitHub is sitting on top of a folder labeled with .hidden.

Under users/ is the boba-tan/ folder, with a subtitle of "your home" and a tilde.
Under boba-tan/ is art/, and to its left is code/. Near code is boba-tan herself
working on her laptop, labeled current position, with a dot. Near the arrow from 
the code/ folder to the boba-tan/ folder is the label "parent directory", with two dots.

On the side a box reads:
Where am I? pwd
What’s here? ls
Move away: cd

If you find this useful (and want a version with better art), let me know! We’re currently working on the cheatsheets for FujoGuide’s backer rewards.

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.

Like what you read?

Tell me about it or help me make more!

Want to help fix something?

Open a PR!

You can also help by sharing this post on socials

Footnotes

  1. While there’s a lot of different types of file systems (you might have heard of FAT or NTFS), their differences don’t generally matter to most users, even programmers.

  2. Theoretically there’s a difference, but practically there’s not. Don’t even worry about it.

  3. Two computer scientists sitting in a tree, T-R-A-V-E-R-S-I-N-G

  4. MacOS + Linux

  5. The ability to use these commands on a terminal is required for POSIX (Portable Operating System Interface) compliance. Using a terminal compliant with POSIX will allow you to use the same commands across any system.

  6. 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.