Skip to main content

Welcome to a Grasp lesson

Create a free account to keep studying and create your own study path.

Lesson 7

Linux File System Navigation

Recommended resources

Welcome to the second module of your journey to becoming a backend engineer. In the first module, we rebuilt your Python foundations, culminating in an introduction to Object-Oriented Programming. Before we dive into more advanced Python concepts, we need to master the primary environment where a backend developer lives and works: the Linux command line.

As a backend engineer, especially at a startup like Infilect, you won't always have a graphical user interface. You'll be connecting to cloud servers, deploying applications, automating scripts, and managing databasesโ€”all through a text-based terminal. Proficiency with the command line is not just a nice-to-have skill; it's a fundamental requirement for efficiency and control.

This lesson will equip you with the essential skills for this environment. We'll focus on the core learning outcome: navigating and manipulating the file system using the Linux command-line interface. You'll learn how to find your way around, create and organize files and directories, and perform the basic operations that form the building blocks of more complex tasks.

1. The Command Line: Your Development Cockpit

The command-line interface (CLI), or "terminal," is a powerful text-based program for interacting with your computer's operating system. It might seem intimidating compared to a visual desktop, but it offers far greater speed, precision, and automation capabilities.

When you open a terminal, you're greeted by a prompt. This is the system's way of saying it's ready for your command. The prompt typically shows your username, the computer's name (hostname), and your current location in the file system.

A typical Linux terminal session, showing the prompt and the output of several basic commands. This is the environment we will master.

Your starting point is usually your home directory, represented by a tilde symbol (~). This is your personal workspace where you have full permission to create and manage your files.

To begin, let's learn the three most fundamental commands for navigation. The following video gives a quick, practical demonstration of the prompt, the home directory, and your first navigation commands.

Basic Linux Navigation

This clip from Nathan Backman's "Basic Linux Navigation" video provides an excellent visual introduction to the terminal prompt, the concept of the home directory, and the essential commands for moving around.

Watch these key segments: The Prompt: This part explains what the different parts of the prompt mean, especially the tilde (~) for your home directory. Listing Contents: Here, you'll see the ls command in action. Changing Directories: This segment introduces the cd command to move into a directory and shows how the prompt updates to reflect your new location.


2. Finding Your Way: pwd, ls, and cd

Navigating the file system from the command line relies on three core commands:

  • pwd (print working directory): Tells you exactly where you are right now.
  • ls (list): Shows you the contents (files and directories) of your current location.
  • cd (change directory): Moves you to a different directory.

The following reading from an Ubuntu tutorial provides a more detailed, step-by-step guide through these commands and the foundational concepts of the file system structure.

The Linux command line for beginners

This guide provides a friendly and thorough introduction to the command line. We will focus on the sections that establish your sense of location and movement within the file system.

Please read the following parts of the tutorial: Start at section 3, "Opening a terminal". This will introduce your very first command, pwd. Continue with the subsection "A sense of location," which explains the concept of a working directory. Read the rest of that subsection to learn the cd command and the special .. syntax for moving to a parent directory. Stop just before the "Relative and absolute paths" heading.


3. Absolute vs. Relative Paths: Your File System's GPS

When you use cd, you need to tell it where to go. You can do this in two ways: using an absolute path or a relative path. This is one of the most critical concepts to master.

  • An Absolute Path specifies a location starting from the very top of the entire file system, known as the root directory (/). An absolute path always begins with a /.

    • Example: cd /home/your_username/projects will take you to the projects directory regardless of where you currently are.
  • A Relative Path specifies a location relative to your current working directory. It does not start with a /.

    • Example: If you are in /home/your_username and run cd projects, it will take you to /home/your_username/projects. If you run the same command from /etc, it will fail because there is no projects directory inside /etc.

To make navigation easier, the shell provides a few essential shortcuts:

Shortcut Meaning Example
/ The root directory cd / (Go to the top level)
~ Your home directory cd ~ or just cd (Go home)
. The current directory mv file.txt . (Move file here)
.. The parent directory cd .. (Go up one level)
This image illustrates several common uses of the `cd` command, showing how to navigate using absolute paths, relative paths, and special shortcuts like `..` and `~`.

Let's return to the Ubuntu tutorial to solidify your understanding of these path types.

The Linux command line for beginners

This section of the tutorial clearly explains the distinction between relative and absolute paths and introduces the ~ shortcut.

Find the subsection titled "Relative and absolute paths" and read it carefully. Try running the example commands at the end to see how your prompt changes.


4. Creating and Manipulating Your Workspace

Now that you can navigate, it's time to start creating and managing your own files and directories.

Creating Directories and Files

  • mkdir (make directory): Creates a new directory.
    • mkdir my_project creates a directory named my_project.
    • To create nested directories all at once, use the -p (parents) flag: mkdir -p my_project/src/components.
  • touch: Creates a new, empty file. If the file already exists, it updates its modification timestamp without changing the content.
    • touch README.md creates an empty file named README.md.

Viewing File Content

  • cat (concatenate): Prints the entire content of a file to the terminal. It's useful for short files.
  • less: A "pager" that lets you view large files one screen at a time. Use the arrow keys to scroll and press q to quit.

Copying, Moving, and Renaming

  • cp (copy): Copies files or directories.
    • cp source.txt destination.txt creates a copy of a file.
    • To copy a directory and its contents, you must use the -r (recursive) flag: cp -r project_a/ project_b/.
  • mv (move): Moves a file or directory to a new location. This same command is also used for renaming.
    • Moving: mv report.txt documents/ moves the file into the documents directory.
    • Renaming: mv old_name.txt new_name.txt renames the file within the same directory.

Deleting Files and Directories

Warning: Commands that delete files on Linux are permanent. There is no "Recycle Bin" or "Trash". Use these commands with extreme caution.

  • rm (remove): Deletes a file.
    • rm temporary_file.txt.
  • rmdir (remove directory): Deletes an empty directory. It will fail if the directory contains any files.
  • rm -r: Deletes a directory and everything inside it, recursively. This is a very powerful and dangerous command. Double-check your path before using it.

The following reading provides concise examples for all these manipulation commands.

Linux Navigation and File Management

This tutorial from DigitalOcean offers a very clear and practical guide to file and directory manipulation.

Read the entire section titled "File and Directory Manipulation". It covers creating, moving, renaming, copying, and removing files and directories with easy-to-follow examples.


5. Hands-On Practice

Theory is one thing, but muscle memory is built by doing. Open your terminal and follow these steps to put everything you've learned into practice.

  1. Navigate Home:

    • Run cd to ensure you are in your home directory (~).
  2. Create a Project Structure:

    • Create a new directory for this course: mkdir infilect_course.
    • Now create a nested directory structure inside it in one command: mkdir -p infilect_course/module_2/cli_practice.
  3. Navigate and Create Files:

    • Change your current directory to the cli_practice folder using a single cd command with a relative path.
    • Verify your location with pwd.
    • Create two empty files: touch notes.txt and touch script.py.
    • List the contents to see your new files: ls.
  4. Organize Your Files:

    • Create a new directory for your notes: mkdir text_files.
    • Move notes.txt into the text_files directory: mv notes.txt text_files/.
    • Check the contents of both the current directory and text_files to confirm the move: ls and then ls text_files.
  5. Copy and Rename:

    • Copy script.py to a backup file: cp script.py script.py.bak.
    • Rename the original script to be more descriptive: mv script.py setup_script.py.
    • List the contents again to see the changes: ls. You should see setup_script.py and script.py.bak.
  6. Clean Up:

    • Delete the backup file: rm script.py.bak.
    • Navigate back to your home directory: cd ~.
    • Now, delete the entire infilect_course directory and all its contents. Be very careful with this command: rm -r infilect_course.
    • Run ls one last time to confirm it's gone.

Conclusion

Congratulations on completing your first essential step into the backend developer's environment. You now have the foundational skills to navigate and manage files directly from the command lineโ€”a skill you will use every single day in your career.

Key Takeaways:

  • The command line is a powerful and essential tool for server management and automation.
  • Navigation is done with pwd (where am I?), ls (what's here?), and cd (let's go somewhere).
  • Paths can be absolute (from the root /) or relative (from your current location . or ..).
  • File manipulation commands allow you to mkdir, touch, cp, mv, and rm your files and directories with precision.
  • Flags like -r (recursive) and -p (parents) modify how commands behave, adding power and flexibility.

In our next lesson, we will build directly on this foundation. We'll explore Git, the version control system that allows developers to track changes to their code, collaborate with teams, and manage project historyโ€”all from the command line.