Explaining how to use the command line and nuances of absolute vs. relative is rather hard to describe and wrap your brain around. To help explain the hieratical nature of Unix and other Unix-like filesystems, I put together a script to render a directory in 3d space. With a cool 3d filesystem to play with, I might as well go over what lives were too.
This script was a bit bigger in scope when I wrote it, and I took info from many places on the web; below, you will find my original script and the sources I used.
The GNU/Linux file system and other Unix-like operating systems can be thought of as a hierarchy of directory starting at “/,” which is usually referred to as root. The files held within these directories contain files to which the system writes data during its operation.
I find it helpful to think of a directory as a location or a room. For this example, files in a directory show up as squares with the height based on file size. Every directory in our current location shows up as a hallway leading to its own room. Building this structure, we can show off the GNU/Linux filesystem in 3d space.
As a side note, at the lowest level, even the directories are just special types of files containing a list of the names of objects like the files in the directory, any links, and other directories.
Here at the start, or ROOT, we have several essential directories. We will go over them one at a time, but before that, let’s talk about your working directory.
When navigating the command line, your console will have a location just like I’ll have a position walking around this 3d filesystem. Right now, my current working directory is / or root. As we move around this virtual filesystem, I’ll show you the relevant commands needed to navigate as well.
To view your current working directory, run PWD.
In modern Linux, this stands for Print current Working Directory. While originally in Unix, this command was read as “Present Working Directory.” Both mean and do the same thing.
Your current working directory is also often sown in your line prompt.
Now that you know that the command line has a working directory location, let’s go over what lives in ROOT.
The boot directory contains all the files needed for your computer to start up. In most cases, this means a grub config file and some UEFI stuff.
Walking down hallways or changing directories is done with the cd command from the shell. Run cd space a directory name in your current directory. This will change your location to the specified directory. If you’re feeling really zesty, cd space ./dirname. The ./ in the previous command means from where I am right now. Remember, where you are right now can be viewed with the PWD command.
As I already talked about these files, as much as I feel I need to, we can leave this room. Just know, you shouldn’t delete these files.
When you want to go back one directory, or in this case, a room. The command can be done relatively, which means from where I am right now, or it can be done absolutely, which means relative to root. This is also called the “full name”. To get back to root with a relative path the command is cd ../. If we were one directory deeper in the filesystem, the command would be cd ../../ Each ../ means back one directory from my PWD.
The absolute way to get back to root is always cd /. If you want to cd to the boot file with an absolute path, the command would be cd /boot. All absolute paths or “full names” start from / and work their way down.
From here on out, I’ll show the absolute path and the relative paths for each command. Hopefully, that will give you an idea of when to use each.
Next up usr
/usr is generally the largest directory on a newly installed system. It is used to store application programs; however, all data here should only contain unchanging static data. Situations like booting a live USB can cause /usr to be moused read-only, this is where /var helps out by always being writable.
Among the various subdirectories within /var is*/var/cache* which contains cached data from application programs. /var/games contains variable data relating to games in /usr. /var/lib contains dynamic data libraries and files. /var/lock contains lock files created by programs to indicate that they are using a particular file or device. /var/log contains log files. /var/run contains PIDs and other system information that is valid until the system is booted again. /var/spool contains mail, news and printer queues. As you can see, /var complements the programs in /usr by giving them a place to write data needed during running.
Another important directory in root is called bin. /bin contains the ready to run programs that are needed to start up the system. It contains the basic command-line tools needed to repair the os in case it brakes. Here you’ll find the shells (like, bash), and a lot of the shell commands like ls, grep, tar, kill, echo, vi. These programs can be used by both the root administrative user and ordinary users.
/sbin is a lot like bin, except its contents are administrative tools that are only available to the root administrative user.
/home contains users’ home directories. You can expect to see a folder here for every user on the system. As this 3d filesystem was created with a Debian based PureOS filesystem, the default installed user is called purism. This folder is where a file browser or command-line shell would open. It contains the standard Desktop, Documents, Music type folders you would expect to see as a Desktop user. As each user has their own home directory, this usually ends up holding mostly personal end-user data like application settings documents, pictures, and Downloads. On the command line, the home folder is often shorted to a ~. This means the same thing as /home/username.
The administrative user on GNU/Linux is also called root, not to be confused by /, with is also called root. The admin root users’ home directory is the only one not in /home and instead, is stored in / itself.
This is more or less the same idea as the directory we just went over, except this is owned by the admin user root.
A few directories are 100% dynamically generated by the kernel. They use special “Magic” filesystems created on startup and don’t exist on the actual harddrive. Mainly these are /dev, /proc, /sys
The /dev tree contains device nodes, which gives user-space applications access to the device drivers in your OS’s running kernel. Or rather, the kernel makes files that represent hardware on the system. All POSIX UNIXY OSes have a /dev. A good example is how the kernel creates a file called /dev/sda for the first harddrive on the system. If you plug in a second harddrive the kernel will create a file called /dev/sdb.
There are, however, several /dev entries that do not correspond to hardware devices. /dev/null, /dev/random, and /dev/tty. These are virtual devices that act a lot like the other devices but don’t point at hardware.
The /proc tree originated in System V Unix, where it only gave information about each running process, using a /proc/$PID/stuff scheme. Linux greatly extended that idea, adding all sorts of information about the running kernel’s status. In addition to these read-only information files, Linuxs /proc also has writable virtual files that can change the running kernel state.
The intended solution for this mess in Linux’s /proc is /sys. Ideally, all the non-process information that got glommed into the /proc tree should have moved to /sys by now, but historical inertia has kept a lot of stuff in /proc. Often there are two ways to effect a change in the running kernel: the old /proc way, kept for backward compatibility, and the new /sys way that you’re supposed to be using now. As a rule, /sys tends to be more strictly organized than /proc.
/mnt should be empty, it’s the recommended place for mounting temporary storage devices, such as USB thumb drives or SSD disks.
/media is the automatic /mnt if you plug in a thumb drive while running a desktop environment like gnome or KDE, it will likely audio mount under media.
/etc is where most of the system configuration files are stored. The hard drives that get mounted on startup and where they are mounted to is stored in /etc/fstab. The DNS servers used by the system are stored in /etc/resolv.conf. If you install a service like apache web server, its’ config files will likely end up in /etc as well.
/opt is usually used for 3rd party programs that don’t use the systems package manager. It’s been a long time since I have seen this directory used.
Lastly, let’s talk about /tmp. This is a place apps can write data that is temporary. For example, an office app might write your document to /tmp when you make edits, then save those edits to a different location when you save the document. The data in /tmp is automatically removed when your system reboots.
Thanks for making it to the end of this deep dive into the GNU/Linux filesystem. Thanks for watching, bye.