How to Navigate the Linux Filesystem with Commands

Minicourse: The Linux Command Line for Beginners | Lesson 2

Welcome back to our Minicourse: The Linux Command Line for Beginners! In this issue, we will dive into the basic usage of the essential commands for navigating the file system: tree, ls, cd, and pwd. These commands will enable you to explore your computer's directory structure with ease. We will also close by looking at the touch command, which is used to create new files.

Understanding the File System Tree

Before we jump into the commands, let's take a moment to understand how the file system is organized. In Linux, the file system is structured like a tree, with a single root directory at the top. The root directory is denoted by a forward slash (/).

Every directory in the file system can contain files and subdirectories, forming a hierarchical structure. Directories are analogous to folders, and files are, well, files! Each file and directory has a unique path that describes its location within the file system.

For example, let's consider a path like /home/user/Documents. Here, we start at the root directory (/), then move into the home directory, followed by the user directory, and finally reach the Documents directory.

Using Commands to Navigate Around the File System

Now that we have a basic understanding of the file system structure, let's explore the commands that allow us to navigate it.

tree - Displaying the Directory Structure

The tree command allows you to visualize the directory structure as a tree. When you run tree without any arguments, it starts from the current directory and displays the entire directory tree structure, including files and subdirectories.

This command can be particularly useful when you want to explore the hierarchy of directories and understand the relationships between them.

Displaying the contents of “my-first-website” and its subdirectories using the tree command

ls - Listing the Contents of a Directory

The ls command stands for "list." It allows you to view the contents of a directory. When you run ls without any arguments, it lists the files and directories in your current directory.

For example, if you're in the /home/user directory and run ls, it will display the files and directories within that location.

Using ls to list the contents of a directory called “my-first-website”

cd - Changing to Another Directory

The cd command is used to navigate between directories. It allows you to move into a specified directory.

To move into a directory, you simply type cd followed by the path of the directory you want to enter. For instance, if you want to enter the Documents directory within /home/user, you would run cd Documents.

Additionally, you can use special notations to simplify navigation:

  • cd .. moves you up one level to the parent directory. It is like using the “back” button on a navigation graphical user interface app (GUI). See the image below for an example.
  • cd ~ takes you to your home directory.
Using cd to go up (“back”) one directory from “my-first-website” to the parent directory “CodeZea1ot”

pwd - Printing the Working Directory (Showing Where You Are)

The pwd command stands for "print working directory." It displays the full path of your current working directory.

Running pwd will show you the complete path starting from the root directory to your current location. This command is helpful for keeping track of where you are within the file system.

Using pwd to print my current working directory

touch - Create New Files in a Directory

Now that we've covered navigation, let's look at creating files in a directory. The touch command is used to create new files. It updates the timestamp of an existing file or creates a new file if it doesn't already exist.

To create a file, simply run touch followed by the name of the file you want to create. For example, touch myfile.txt will create a file named myfile.txt in your current directory.

Coming Up Next

In the next issue, we'll explore the powerful command line text editor called Vim and learn how to use it to edit the file we created using touch.

So, go ahead and create a file using touch to prepare for the next episode. Feel free to experiment and get comfortable with the commands we've covered so far.

That's all for this issue! In the next episode, we'll dive into the world of Vim and discover how to edit files like a pro. Stay curious and keep exploring the Linux command line!

Happy navigating!