Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Heirarchical File System

The Heirarchical File System, or Directory Tree, is how files are organized on most computers, including yours and most servers you visit online. In Linux, everything is treated as a file - files, directories, programs, devices, configurations, everything. Understanding this structure allows you to be deeply familiar with your system, manipulate it, and remain organized with your data.

As the name implies, your file system works as a heirarchy. This is described as a tree because every directory begins at a common root and ‘branches’ out. Although the inverse of a real tree, this structure is traditionally conceptualized as ‘top-down.’ So when a file is in a directory, that directory is said to be ‘above’ the file, closer to the root.

Important

You will now begin to see what are known as “code blocks.” This is indicates it is text from a terminal shell or code environment. The different format, often on its own line, makes it easy to find/copy when working from another window: code block example

Don’t worry about the various colors for now, we’ll talk about that later.

Caution

When copying from a code block, it MUST BE EXACT. Typos in commands and configurations will almost certainly cause the majority of your problems.

All directories/files stem from the root, which we symbolize with the / symbol. If your root (/) contains a directory called home, then that would be located at /home. If /home contains a directory called user, then that location would be /home/user (the / symbol also separates directories, and we pronounce it “slash”). Let’s jump into a terminal and get familiar.

We’re going to get started in a terminal. There are many reasons for this, namely, you will at some point need to drop in here as a Linux user. If you find it painful, good, embrace it. Not everyone will become a command wizard, and that’s fine, you just need to know the basics. We will mix in graphical actions shortly.

Basic Navigation

Tip

There are usually many ways to do something on a computer. Learn a few and use the methods you prefer.

Let’s plug in. On your Linux machine, open a Terminal. This is commonly mapped to the keyboard shortcut: ctl+alt+t (meaning you hold down the ctl and alt keys and then tap the letter t). It can also be found in your applications drawer, main menu, or system tray. Try out the following commands (all executed by using the Enter or Return keys) to learn shell navigation.

You will see what is known as the “prompt,” followed by a flashing cursor. Sometimes it is simply a $ or #, and other times it may be used to identify some information. In our example below, it lists the user@host (tester@zorin-testing). The host is simply the name of this computer (used to identify it on a network). Terminal

  1. pwd
    
    • ‘present working directory’ - this will tell you where you are, likely /home/USERNAME

    Tip

    When you see a codeblock containing all caps, like USERNAME above, it often means that your machine will show something unique, in this case, the name of your user.

  2. ls
    
    • ‘list’ - this will list the contents of the directory you are currently in. If nothing is returned, the folder is empty.
  3. ls -a
    
    • ‘list all’ - here we have passed an ‘option’ to our command. The -a option will list ‘hidden’ files, which are indicated with a . in front, such as .bashrc. If dictated, we pronouce the - as a “tack.”
  4. mkdir testing
    
    • ‘make directory’ - creates a folder called testing (you can verify its existence with ls)
  5. touch testing/test0
    
    • ‘create file’ - creates a file called test0 - note that it is in the testing directory. Use ls testing/ to view the contents.
  6. cd testing
    
    • ‘change dir’ - moves into the testing directory, equivalent to ‘opening’ a folder in the GUI. Your prompt may change to reflect the new location, if it doesn’t, you can verify with pwd or check contents with ls.
  7. rm test0
    
    • ‘remove’ - deletes the test0 file. This is permanent.

After all this, your terminal may look something like this demo:

Basic Commands

Great, now let’s take a look in a graphical file explorer to visualize all this.

Note

Directory and folder are interchangeable terms.

  1. Open up your file explorer. For this demo, we are using Zorin (Gnome DE), and the explorer is simply called “Files.” On your machine, it could be different. For example, the KDE explorer is called “Dolphin.”

    • Usually this will open your user’s home folder by default

    Files

  2. You’ll notice the testing folder we created is here, click into it.

  3. We deleted our test file, so the folder is emtpy. Right-click and create a new file of any kind called test1 (right-click the file to rename). The file will appear in the terminal when you ls testing or simply ls if you’re already in that directory.

File Manipulations

Let’s learn some more useful commands.

  1. Go back to your terminal (or open a new one), and navigate to the testing directory, if you aren’t there already.

    cd ~/testing
    

    Tip

    The tilde (~), which we call the ‘twiddle,’ is a shortcut for the current user’s home directory (/home/YOURUSER).

  2. Let’s attempt something that is not possible - to move a file that doesn’t exist:

    mv test9 ..
    
    • mv is the ‘move’ command, and here we are moving the test9 file (which doesn’t exist) to the directory ‘above’ (indicated by the .. shortcut) the one we are currently in. Unless you happen to have a file by that name in this directory, you will get an error something like this (circled in red):

      ![No Such File](../images/ops-101-2/zorin-term-error.png

    • This directly tells you that the mv command can’t find the file you asked it to act upon.

    Caution

    READ ERRORS! Developers create errors specifically to help you deal with problems that they could not otherwise eliminate - such as the most common, human errors. You will see errors in most kinds of software, and reading them will very often provide you a solution.

  3. mv test1 ..
    
    • Presuming you created the test1 file, the file will now have been moved from /home/YOURUSER/testing/ to /home/YOURUSER - you can verify this with ls ~ or in your graphical file explorer.

    Moved File

  4. cd ~ && cp test1 testing/test-copy
    
    • Here we are chaining two individual commands. First we change our working directory to ~ (/home/YOURUSER), ‘and then’ (&& - the logical AND - it will execute the second command only if the first succeeds) we ‘copy’ (cp) the test1 file into the testing/ directory, with the name test-copy.

    Note

    You may have noticed that commands are generally separated by spaces. This is known as a ‘delimiter,’ and acts as a border. Think of this as ‘something ends/begins’ with a delimeter. You will see more styles of delimiters (tabs, punctuation, etc) in the future.

  5. rm test1 && cd testing
    
    • Delete the original file and then change directories into testing.
  6. echo "This is a test." > test-copy
    
    • ‘print’ (echo) everything within the double quotes ("") and ‘redirect’ (> - overwrite) it to the file test-copy. The redirect will overwrite the entire file, so use with caution. If you want to append (write to the end of) a file, use the >> operator.
  7. cat test-copy
    
    • ‘concatenate’ (cat) the test-copy file. The name means that we can join multiple files and display them, but this is also a nice quick tool to display the contents of a single file. At this point your session might look something like this:

    Terminal Commands

Great work for making it this far! You now know how to navigate a shell session in a terminal and do some basic file manipulations.

At this point, it may seem to you that this was a lot of work for not a lot of result. Perhaps like an early math class you may wonder - when will I ever use this? The answer is difficult to ellucidate, but know that this will help you throughout your journey. If you intend to continue down the tracks of infrastructure, hacking, or programming - these baby steps will be vital to you going forward, and you will want to master them immediately.