Linux Quick-Start Guide: the File Hierarchy

Joseph Syverson
Nov 28, 2019 · 6 min read
Source

Note: a detailed description of the Linux file system is forthcoming. When I write it, it will be linked from the title of this section (The file hierarchy). For now, the manual can tell you about what’s in the folders that appear in the graphic above. Type man hier for a very concise description about what we’re dealing with — 2019/11/18

Note: the term “directory” and “folder” are used anonymously in this article. They are the same thing. “Directory” is an older term that is still primary for Linux users, while “folder” is an analogical euphemism popularized for commercial operating systems. It is also a very old term that has been used for decades.


Orienting oneself: as in real life, it’s hard to accomplish a goal without knowing who and where you are.

When you open up a terminal, you arrive with an identity to a location, associated with the machine and it’s personality. Some of the following information may seem irrelevant because the default prompt for your terminal lists it automatically, but you may find yourself on a machine that does not provide the information up front.

To figure out which user you are, type:

whoami

Output your currently location by typing:

pwd

Typing date will give you the date and time. Type uname -a for specifics about your computer’s operating system.


The file hierarchy: in Linux, just about everything is represented as either a file or a directory. Your computer is a landscape of files that you can traverse using BASh. This is especially attractive to those with a background in programming because your terminal provides an interface for navigating and programming a living A P I.

The graphic at the head of this article shows the Linux file hierarchy from root. root is the very base of your machine’s software. It’s where the operating system lives, in addition to numerous files representing just about anything your computer has anything to do with, from hardware on your computer (yes, Linux represents hardware as programmatically accessible and mutable files) to processes (yes, Linux represents all processes currently running on your computer as programmatically accessible and mutable files).

root is represented by a / in your terminal. Inside of root there is a folder called home. The path to home folder is therefore /home. This is where the data for all the regular users of a computer store their data. When you log into your computer, you’re immediately sent to /home/<user>, where <user> is your username.

To see the files and directories in the directory you’re in, type:

ls

If you want to know what’s in a directory without having to go all the way there, type ls <path to directory>:

ls /

ls lists all files that are not prefixed with a dot. Files prefixed with a file are hidden from standard view for convenience and security (E G, .bashrc). If you want to list all files, regular or hidden, type:

ls -a

From anywhere, you can look at the paths and contents of any other location on your computer, so long as you have the administrative rights to do so (see the Power Tools section for basics about user privileges). For an in-console visualization of the files of a given directory, type tree <path to directory>. For example:

tree /home

(If you don’t have Tree installed, see Power Tools).

This is what a relatively fresh install of Ubuntu looks like treed

I guess it’s not a huge surprise that the <user> folder is where is where most users spend they’re time. They’re sent to their user folder when they turn on their computer and never leave. This is as it should be unless you have a special task, work in I T, or are a computer enthusiast. The image above shows some of the important folders that come with desktop Linux distributions out of the box. Notably Documents, Downloads, Music, Pictures, and Videos.


Find whatever: if the file exists, you can get at it even if you don’t know where it is. Linux distributions come with a variety of sophisticated tools for locating what you’re looking for. Below are a few basics that will grant the average user as much as they’ll ever need to know.

To find files or directories, you have a number of options. The find command offers a sophisticated searching program that can query for any criteria that defines a file, whether a simple search like a file name; to a complex one, like all the files owned by given user that are executable for members of the user’s group, but no one else. It deserves it’s own tutorial. In the spirit of this blog, however, we’ll discuss locate instead. If you know any piece of the file name that you’re looking for, combine locate with that piece, IE locate <piece of file name>. For example:

locate syverson-resume

locate will return the paths of all files all files and directories that include what you searched for.

Note: locate is faster than find because it references a database file that stores file paths. Therefore it is not live. If a file is created, it is not stored in the locate database until that database is updated. You can update the database with the updatedb command, which is installed upon installing locate. locate's database comes pre-configured to search only in certain directories and certain files in those directories. The directories and file types specified are those that you would often search in for administrative tasks. You can modify the config, which may often be advisable for administrators, but add too many exceptions and you get a large and increasingly unwieldy database. The purpose of locate is that it provides fast and easy reference. If you’re digging for something special, like a group of related but apparently disparate files, just use find.

If you’re just looking for a program, you can find them by typing which and the command that you would call in your terminal: which <command>. For example:

which firefox

Finally, you can search the contents of files and the output of programs with grep. Like locate, the syntax takes a string that serves as the basis of the search: grep <search term> <file name>. For example:

grep PS1 /root/.bashrc

In the instance above, we’re searching for the string “PS1” in the .bashrc file in root's user directory. The point of the command in this instance is to see if a file references something that we care about without having to read through the entire file. If you’re not in the habit of digging through strange files in your root directory, then this tool may not seem immediately practical.

As a programmer, I find myself runninggrep on the output of a command more often using the | character (read more about | in the Programming section). For example:

ps -aux | grep <search term>

ps is a command that lists all current processes (see Deeper Views of the System for more information). It’s a long list, so we use grep to narrow down the search. If we type ps -aux | grep firefox, ps will first prepare a list of processes, then pass that list to grep. grep then uses that list as the basis for its search (firefox). If Firefox is indeed running, all of Firefox’s processes will be returned as a list.

A use case: you’ve sent your JavaScript program into an infinite loop and it’s not responding to the graphical method of quitting. Every millisecond your processor and memory usage increase and if you don’t kill your browser, your computer will crash. You can find it’s process I D with the command above, then kill it. For full instructions on how to do this, see Deeper Views of the System.

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade