Skip to content

Lesson 1: Understanding the file system

If you have used Microsoft Windows, you'll remember that the file structure goes something like C:\somefolder\anotherfolder, where C:\ is the parent, or "root" directory -- the directory that contains all other files and directories.

On Linux and macOS, instead of C:\, the root directory is simply / (forward slash). The file structure here is /somefolder/anotherfolder.

Alert

On Windows, the directories are nested with a backslash (\), while Linux and macOS use a forward slash (/). If you use a backslash on Linux or macOS, the commands simply won't work.

Home sweet home

By default, you start in the "Home" directory. This is where your personal files are stored. Let's find out where this directory is located in the file system and also learn your very first command!

In your terminal, type in pwd (shorthand for present working directory), and press Enter/Return. You should see something like this:

> pwd
/home/selfhostwiki
> pwd
/Users/selfhostwiki

Alert

Terminal commands are case sensitive, i.e., pwd (all lowercase) will work, but Pwd, PWD or pWD will not.

Let's unpack what's happening here.

When you press Enter after the pwd command, your system understands it as, "identify the name and path of the present working directory and print it on the screen". The output on macOS is slightly different than on Linux, so each gets its own section:

On Linux, you'll see something like /home/<your username>. In my case, the username is selfhostwiki, so the output is /home/selfhostwiki. For you, it might be /home/mark, /home/jessica, or whatever you chose as your username while setting up the system.

Think of it like: the / (root) directory contains the home directory, which in turn contains the <your username> directory.

On macOS, you'll see something like /Users/<your username>. It might be /Users/mark, /Users/jessica, or whatever you chose as your username while setting up the system.

Think of it as: the / (root) directory contains the Users directory, which in turn contains the <your username> directory.

For the sake of brevity, only the Linux version of the Home folder is referenced for the rest of the course. Whenever I use the Linux convention /home/username, translate it as /Users/username if you use a Mac.

With that out of the way, let's see what your Home directory contains and learn the second, much more commonly used command.

Listing files in a folder

Now that you know your place in the universe filesystem, it's time to see what files and directories your Home folder contains.

Type in ls (shorthand for list) in your terminal, and press Enter. You should see something like this:

> ls
Documents  Downloads  Music  Pictures

When you run the ls command, your system understands it as "List all the files and folders in the current directory". Since you are in your Home directory, it prints the standard folders like Documents, Downloads, along with any other files and folders that it contains.

Peeking into other folders

Now that you know how to list files in the current folder, let's see what your Downloads folder contains.

Type in ls Downloads and press Enter. Remember that file names, just like commands, are case-sensitive. Make sure you use the exact case and spellings that you saw in the output of the ls command you used in the previous step.

> ls Downloads
'Africa by Toto.mp3'   selfie.jpg   syllabus.pdf

Here, ls is the command we use, and Downloads is what's called an argument for the command. This tells ls to list the contents of the Downloads folder. In my case, the folder contains the files "Africa by Toto.mp3", "selfie.jpg", and "syllabus.pdf".

Your Downloads directory will probably have a different set of files, depending on what you have downloaded from the internet. You might even get a blank output here. Do not fret, this just means that your Downloads folder does not contain any files yet.

If you, like myself, have hundreds of uncategorized files in the Downloads folder, use Shift-PageUp / Shift-PageDn on Linux, or fn-Up arrow / fn-Down arrow on macOS to scroll up and down to see the complete list of files.

Exercises

  1. Run the ls command with other folders such as Pictures or Documents instead of Downloads.
  2. Run ls with multiple arguments. For example, ls Downloads Pictures Music. What do you see?
  3. Run ls /home/<your username>, followed by ls ~. Do you notice a difference?
  4. Similarly, run ls ~/Downloads.

Doubts and explanations

Running a command with multiple arguments

When you run ls Downloads Pictures Music, the system understands it as, "List the files and folders that the Downloads, Pictures, and Music directories contain". You can pass multiple arguments (space separated) to almost any command. A sample output is:

> ls Documents Pictures Music
Documents:
document1.docx

Music:

Pictures:
MonaLisa.jpg  screenshot1.png

What's the deal with the ~ symbol?

~, or tilde, is shorthand for the currently logged in user's Home directory. When the user mark uses ~ as an argument for ls (or any other command), it translates to /home/mark. If the user jessica is logged in and uses the same symbol, it translates to /home/jessica.

For most purposes, ls ~ is identical to ls /home/username.

There are more symbols that make life easier on the command line. You will learn them in later chapters.