💾 Archived View for yujiri.xyz › software › guide › filesystem.gmi captured on 2024-08-18 at 17:30:41. Gemini links have been rewritten to link to archived content

View Raw

More Information

⬅️ Previous capture (2024-05-10)

-=-=-=-=-=-=-

yujiri.xyz

Software

Learn programming for good instead of profit

Filesystem concepts

written 2023-06-03

Here I'm gonna talk about concepts like files, folders, paths, partitions, and mount points.

Files and folders

This is pretty basic information, but I don't want to leave any gaps, so I'll explain it even though you probably already know.

A file is an object stored on your storage device. They're the things that show up in your file explorer program or on your desktop. Fundamentally, a file is a series of bytes with a name and some other properties.

A folder is a container of files, much like a physical folder. It's usually called a directory on Unix-like operating systems; it means the same thing.

Historical note on Unix, POSIX, and Linux

All the files on your computer are organized hierarchically, like a tree, with folders that contain other folders. For example, on most operating systems you have a "home folder" which contains folders like Documents, Downloads, and Desktop, which in turn contain various files.

The "root" folder, written `/` (or `C:\` on Windows), is the one that ultimately contains everything else.

Paths

A file *path* is a series of folders that specifies how to get to a specific file. Folders are separated with slashes. For example, on Linux the path to one your files might be `/home/bob/Downloads/meme.png`. This means you have a folder just inside root called `home` (which contains separate home folders for any number of users), a folder inside that called `bob` which is the home for a specific user, a folder inside `bob` called `Downloads` and a file inside `Downloads` called `meme.png`.

Paths that start from the root are called absolute paths. There's also relative paths, which are interpreted as starting from wherever you currently are. For example, if you're in `/home/bob`, the relative path `Downloads/meme.png` means `/home/bob/Downloads/meme.png`. But if you're in `/home/alice`, it would mean `/home/alice/Downloads/meme.png`.

The special path `.` means the current folder. If you're in `/home/bob`, `.` means `/home/bob`, and `./meme.png` means `/home/bob/meme.png`.

The special path `..` means the parent of the current folder. If you're in `/home/bob`, `..` means `/home`, and `../alice` means `/home/alice`.

Extra slashes in a path typically don't mean anything. For example, Linux interprets `/home//bob` the same as `/home/bob` (but some programs might get confused by it). A trailing slash, like `/home/bob/`, is also typically optional, but it does mean that the path is a folder. If you try to open `meme.png/`, you probably get an error saying "Not a directory".

Soft and hard links

If you've used Windows, you might be familiar with the concept of a shortcut. A soft link, also called a symbolic link or symlink, is the same thing on Unix. It's like a pointer to a file. If you have a file called `a` and you make a soft link called `b` that points to `a`, accessing `b` is just like accessing `a`. You could open `b` in a text editor, make some changes, and then open `a` and see those changes reflected. You could also open `a`, make some changes, then open `b` and see those changes reflected.

Symbolic links are sometimes used for accessing programs that have multiple versions without caring about which version you access. For example, you might have `/bin/python` on your system, which is a symbolic link to `/bin/python3.11`. Python 3.11 is the version you have installed, but you also have a link to it just named "python" so you can effectively say to your computer "open whatever version of Python I have installed".

Soft links are not interchangeable with the original file though. If `b` is a soft link to `a` and you delete or move `a`, `b` becomes a "broken link" and is now useless. You can't open it because the thing it points to is gone.

A hard link is a little different: it's really just another name for the same file. If `b` is a hard link to `a`, then it's also correct to say that `a` is a hard link to `b`. You could delete either one of them and the other will still work.

In fact, you could move *both* of them without breaking the link between them. The reason this works is because a hard link doesn't just point to another file *by name*. It points directly to a place on disk, like a normal file. It just happens to point to the same place as something else.

Filesystem

A filesystem is a way of arranging files on a storage device. If you wonder what challenges a filesystem faces, consider things like:

A filesystem is an answer to these questions. As the questions suggest, filesystems are very complex and difficult to design well. Most operating systems support multiple filesystems, but have one that they use by default for your main storage device. On most Linux distributions, the default filesystem is either ext4 or btrfs.

The word 'filesystem' can also refer to an instance of a filesystem. For example, all the files on your storage device are said to be in your filesystem (might be more than one, see the next section).

Partitions

Storage devices are usually divided into a few sections, called partitions, that each have their own filesystem. As an example, if you go through a Linux distribution's guided installer with the default settings, it'll probably create:

If you're a tinkerer, or depending on the distribution's installer, you might have a different layout. For example, your primary partition might be divided into a partition for installed packages and a partition for user data. I won't go into the pros and cons of these choices.

Mount

To mount a filesystem means to tell the kernel to make it available at some path underneath `/`. The one mounted directly at `/` is your main filesystem. Your boot partition's filesytem might be mounted at `/boot` (so things under `/boot` are files from the boot partition, not your main one). If you have a separate partition for user data, its filesystem is likely mounted at `/home`. If you have a flash drive connected, it might be mounted at `/mnt` or `/media`.

The `mount` command on Unix is used to manage filesystem mounts. If you run it by itself it'll show all the mounted filesystems. If it shows you a big list, probably most of them are virtual filesystems that don't represent actual files on storage. For example, the one mounted at `/dev` contains virtual files that represent hardware devices like your storage, mouse and keyboard. The one mounted at `/proc` contains virtual files that represent running processes.