All files have a name. Like you, all files have a first name and a last name. A file's first name might be unicorn_drawing.jpg or the-lick.mid, just like maybe your first name is Susan. All files have a last name, too.

If you're inside of your Pictures folder, you might find a file called horsey.jpg. Great! Let's delete it.

jesus@chariot:~/Pictures$ rm horsey.jpg
jesus@chariot:~/Pictures$

Success! We hate horses. But what if you have multiple files called horsey.jpg on your computer? Thankfully, each file has a last name as well, which is determined by the file's parent folders. If we open the Pictures folder in the computer's file browser, we can keep going one folder up until we hit the end. Here's what I found:

  • horsey.jpg was inside the Pictures folder,
  • which is inside the jesus folder,
  • which is inside the home folder,
  • which is inside the root directory (/).

So, the full name of horsey.jpg would be:

/home/jesus/Pictures/horsey.jpg

We can confirm this using the readlink -f command. Just pass in a filename to get the file's full name.

jesus@chariot:~/Pictures$ readlink -f horsey.jpg
/home/jesus/Pictures/horsey.jpg
jesus@chariot:~/Pictures$

With a file's full name there's no question about whether horsey.jpg is the picture of the stallion inside of your Pictures folder, or the picture of the seahorse inside the Downloads folder. A file's full name is also an absolute path. It's "absolute" because there's no question about exactly which file we're referring to.

What's a path?

A path is a direction for finding a file. Imagine you're lost in a new city, you might ask a local "Excuse me, do you know where 34th St is?" and they'll point in a direction and say "West, 4 blocks."

Likewise, say we're back in the Pictures folder again and we want to delete that pesky horsey.jpg file in your Downloads folder. We can do so like this.

jesus@chariot:~/Pictures$ rm ../Downloads/horsey.jpg
jesus@chariot:~/Pictures$

Here, ../Downloads/horsey.jpg is a relative path. We are saying

  • starting where you currently are (Pictures), "go up one folder" (.. is shorthand for this),
  • then down into the Downloads folder,
  • then to horsey.jpg.

Great! It worked. But maybe there's still a horsey.jpg file in a  faraway place. Imagine instead you'd asked "Where's Shanghai?" and the friendly pedestrian said "The universe. The Milky Way. People's Republic of China. Shanghai." That's a pretty absolute path! So maybe we'd have to delete a file like this.

jesus@chariot:~Pictures$ rm /opt/SquiglioApp/creatures/not_fungus/mammals/horsey.jpg
jesus@chariot:~Pictures$

Absolute paths always start with a forward-slash, which means the root of the hard drive. There's nothing beyond the universe (that we know of) and equally there's nothing beyond the root of the hard drive (/). It's the starting point for everything, and so absolute paths are based upon it.

There's a folder called opt inside the root of the hard drive, and we descended through it - first into SquiglioApp then creatures then not_fungus then mammals - to find our horsey.

Let's confirm that opt exists within the root of the hard drive. We'll use the ls command.

Using ls with an absolute path (eg  ls /) will make ls follow the path before listing the files.

jesus@chariot:~$ ls /
bin    core  home            lib    lost+found  opt   run   sys  var
boot   dev   initrd.img      lib32  media       proc  sbin  tmp  vmlinuz
cdrom  etc   initrd.img.old  lib64  mnt         root  srv   usr  vmlinuz.old
jesus@chariot:~$

You can clearly see opt in there (top row, third from the right).

Path vs filename. Same thing?

A filename is exactly what it sounds like. Technically speaking, all files only have one name - the "full name". Our computers perform some magic to make you feel like you're clicking through folders when a file has slashes, but the hard drive itself stores files in a flat, non-hierarchical way. horsey.jpg as the "first name" only exists as a social construct with a matching user interface. Neat!

A path is not a filename, it's an instruction for how to reach a file. This filename: /home/jesus/Pictures/horsey.jpg happens to match this path: /home/jesus/Pictures/horsey.jpg in a literal sense, but a filename and a path are still semantically different depending on the context in which you're talking about it. Are you referring to the full name of a file itself or are you asking for directions on how to get there?

The home directory (~)

On Linux, users typically have a home directory. This is a folder on the hard drive just for that user. While logged in as yourself, you can use the special tilde symbol to access your own home folder. For instance, changing into my home directory:

jesus@chariot:~/Pictures$ cd ~
jesus@chariot:~$

Using the pwd command, you can find the absolute path of the directly you are currently in. Let's try that for our home folder.

jesus@chariot:~$ pwd
/home/jesus
jesus@chariot:~$

It says /home/jesus.

Therefore, ~ resolves to /home/jesus, and both paths are functionally the same. Tilde is a nice shorthand, and can also be used to write scripts that are agnostic to the user who is running the script, since ~ will always resolve to the home directory of the current user.

In most systems, the home folder is stored at /home/<username>. Multiple users can exist on the same system and multiple home folders. As a special case, the home folder of the root user is /root.

The Linux filesystem

So, what is all that stuff in the root directory, and why? There are some variations between different Linux distributions, but more often than not things are consistent. Yay teamwork!

The Linux Foundation developed what they call the Filesystem Hierarchy Standard (FHS) which is a document that describes how Linux-based operating systems should organize their files.

Directories like /etc, /opt, /var, /home, /usr etc each have a specific purpose. Rather than listing them all here, I'll direct you to the FHS Wikipedia article which has the full list.