The UNIX Filesystem
Many operating systems today are "UNIX-like" systems. This means they follow the standards of UNIX, but don't use the proprietary source code of UNIX.
Among these standards is the UNIX Filesystem. This is used by operating systems like MacOS, Android (which runs the Linux kernel under the hood), and Linux.
If one wishes to do low-level programming for these systems, one must understand this filesystem standard (and if you use Linux it's common knowledge).
Let us now look at an example line of `ls -l` output.
'drwxr-xr-x 2 root root 4096 Mar 3 14:05 gtk-2.0`
That must be a lot to digest. The first column, `drwxr-xr-x` is the permissions. The first letter, "d", indicates that this is a directory.
`r` here stands for "read permissions"; `w` here stands for "write permissions"; and `x` here stands for "execute permissions".
But why do they repeat? Well, let's split them into groups of 3 letters.
1. rwx
2. r-x
3. r-x
The first group is user permissions; which define what the user who owns the file can do with it.
The second group is group permissions; which define what the users in the owning group can do with the file.
The third group is permissions for others; basically anyone who isn't the owning user or part of the owning group can do these with the file.
Did you know that we just dissected a binary number? If you view these permissions in binary, this is what you get:
111101101
Furthermore, if we compress each group of permission bits into "Octal" (base 8), this is what we get:
755
This octal format is the standard for viewing these permissions.
As for the initial letter in the output, you can also have "normal file" (-), or "symlink" (l). There's also others, but it depends on which filesystem implementation you're using.
The second column of the output references how many links there are to the file in question. There are 2 types of links in the UNIX filesystem: Symlinks, and Hardlinks. Symlinks, or Symbolic Links
point to a particular path in the filesystem. Hardlinks point directly to an "inode" (a place on the disk where a file is stored), meaning even if the original file is moved elsewhere, the hardlink won't break;
but the symlink will.
Next part is easy to understand. The third column is the user who owns the file. In this case, it's root (which is a special user with maximum permissions). The fourth column is the group owning the file, which is
the root group. However, the root group doesn't have the same special abilities as the root user.
There's something important to understand about the UNIX filesystem: Everything is a file. Even folders are files. Device entries are also just special files. Everything being handled as a file makes sense, because under the hood it's all just chunks of data, anyway.
The 5th column shows the size of the file, which in this case is 4096 bytes.
The 6th column is the modification date & time. Pretty self-explanatory. The 7th and final column is the file name, which in this case is `gtk-2.0`.
Those have been the basics on how a file works in the UNIX filesystem, but now we will discuss how directories work. Here is an example absolute filepath: "/home/user/.local/share/". Notice how the start of the path doesn't have anything like "C:", and how it also uses forward slashes. If you go to just "/", you are at the "root" directory (Not to be confused with the root user's home located at /root). From here, if you go to "/home", you are in a directory which leads to the homes of every normal user. Now, if you go to "/home/user", you are in the user "user"'s home directory. If you use `ls` here, you won't see ".local", though. This is because files prefixed with "." are hidden files, which is a QOL feature to make the system look less cluttered. You can see the folder though with `ls -a`. Now's a good time to get to non-absolute paths. If, from here, you go to the directory "./.local", you'll end up in "/home/user/.local". This is because "." on it's own stands for the current working directory. Therefore, putting a forward slash and a directory name in front of it will make you go to that directory (if it exists). ".." is another such thing, and it stands for the previous directory. If you're in the directory "/foo/bar" and "/foo" also contains the directory "baz", then "cd ../baz" will take you to "/foo/baz". Finally, there's "~". If you use it on it's own, it stands for your home directory. If you're "user" this would be "/home/user". You can also append a username to it to stand for someone else's home directory.
(e.g. ~foo = /home/foo)
Finally, we will talk about how disks are mounted. Unix filesystems have a special directory called "/dev" which contains a lot of special device files. Among these are "block devices", which are used to access disks/drives
and equivalent hardware/software storage. The name the kernel gives to the drive depends on what drive it is, but we'll just assume that it's using the SATA bus format (/dev/sdX). At boot time, the kernel must mount the
root filesystem on "/", and from there anything else is mounted on a path in the root filesystem. (e.g. mounting /dev/sda1 on /boot). Mounting additional filesystems overrides the presence of any files from the
root filesystem which are in the mountpoint, and in their place is the contents of that block device.
That's everything for now. This tutorial will be updated periodically to fix any mistakes or to add missing info