Navigate the Filesystem in Linux

CompTIABeginner
Practice Now

Introduction

In this lab, you will learn the fundamental skills required to navigate the Linux filesystem using the command line. You will be introduced to essential shell commands such as pwd to print the working directory, cd to change directories, and ls to list the contents of a directory. These commands form the foundation for interacting with any Linux-based system.

Throughout the exercises, you will practice moving between directories using both absolute and relative paths. You will also explore useful shortcuts like tilde expansion (~) to quickly access your home directory and use command options like ls -l and ls -a to view detailed file properties and hidden files. By the end, you will have a solid foundation for working effectively in a Linux shell environment.

Identify Your Home Directory with pwd and cd

In this step, you will learn two fundamental commands for navigating the Linux filesystem: pwd and cd. We'll start by identifying your current working directory and then learn how to move to your home directory.

The pwd (print working directory) command tells you your current location in the filesystem's hierarchy. When you start a new terminal session in this lab, you are placed in a specific directory called project. Let's verify this.

Execute the pwd command in your terminal:

pwd

You should see the following output, which is the absolute path to your current directory:

/home/labex/project

Next, let's explore the cd (change directory) command. This command allows you to move between directories. Every user on a Linux system has a special "home" directory, which is the default location for storing personal files. For your user, labex, the home directory is located at /home/labex.

A convenient shortcut to navigate directly to your home directory from anywhere in the filesystem is to type cd without any arguments. Let's try it now:

cd

This command typically produces no output, but it silently changes your location in the background. To confirm that you have moved, use the pwd command again:

pwd

The output will now show the path to your home directory:

/home/labex

For the remainder of this lab, we will perform our work inside the ~/project directory. Let's navigate back to it. The tilde character (~) is a special shortcut that represents your home directory. Therefore, ~/project is a path that starts from your home directory and points to the project directory inside it.

Execute the following command to return to the lab's working directory:

cd ~/project

Finally, run pwd one last time to ensure you are back in the correct starting directory for the next step.

pwd

You should see this output, confirming you are back where you started:

/home/labex/project

You have now successfully used pwd to see where you are and cd to move to your home directory and back. These are essential skills for working on any Linux system.

Change Directories Using Absolute Paths and Tilde Expansion

In this step, you'll expand your navigation skills by using absolute paths and tilde expansion. These are powerful shortcuts that allow you to move to any directory from anywhere in the filesystem with a single command.

An absolute path is a path that starts from the root directory, which is represented by a single forward slash (/). It provides the complete location of a directory. Let's use an absolute path to navigate to the /etc directory, which contains system configuration files.

From your current directory (~/project), execute the following command:

cd /etc

To confirm that you have successfully changed directories, use the pwd command:

pwd

The output should show the new location:

/etc

Now, let's learn about tilde expansion. The tilde character (~) is a special shortcut for your home directory. As you saw in the previous step, cd ~ is equivalent to cd /home/labex. You can also use the tilde to navigate to another user's home directory, but this requires appropriate permissions.

Let's demonstrate tilde expansion by navigating to a directory within your home directory. First, let's go to your home directory:

cd ~

Verify your location with pwd:

pwd

The output will show:

/home/labex

Finally, let's return to our working directory, ~/project, using the tilde shortcut for your own home directory. This is a very common and efficient way to navigate back to your project files.

cd ~/project

Run pwd one last time to ensure you are back in the correct directory for the next step.

pwd

You should see the familiar output:

/home/labex/project

You have now mastered changing directories using both absolute paths and the versatile tilde expansion, significantly improving your command-line navigation efficiency.

List Directory Contents with ls and ls -a

In this step, you will learn how to view the contents of a directory using the ls command. You will also discover how to view hidden files, which are an important part of the Linux filesystem.

First, ensure you are in the ~/project directory. Let's create a couple of files to work with so we have something to list. We'll use the touch command, which creates an empty file if it doesn't exist.

In your terminal, execute the following commands to create one regular file and one hidden file:

touch sample.txt
touch .hidden_file

Now, list the contents of the current directory with the basic ls command:

ls

You will see that only the regular file is listed:

sample.txt

Notice that .hidden_file is not displayed. This is because, by default, ls does not show files or directories whose names begin with a dot (.). These are known as "hidden" files or "dotfiles" and are often used for user-specific configuration.

To see all files, including the hidden ones, you need to use the -a (or --all) option with ls. Let's try that now:

ls -a

This time, the output shows everything in the directory:

.  ..  .hidden_file  sample.txt

Now the output is different. You can see our .hidden_file. You also see two special entries: . which is a reference to the current directory itself, and .. which is a reference to the parent directory (the directory one level up). You will learn more about using .. to navigate in a later step.

By comparing the two outputs, you can clearly see that ls -a provides a complete listing of all directory contents, which is crucial when you need to find configuration files or other hidden items.

View Detailed File Properties with the ls -l Command

In this step, you will learn how to get more information about files than just their names. The ls command has a powerful option, -l, that provides a "long listing" format, showing detailed file properties like permissions, ownership, size, and modification date.

Ensure you are in the ~/project directory where you created files in the previous step. Now, execute the ls command with the -l option to see a detailed view of the non-hidden files:

ls -l

The output will be much more detailed than before. You will see a line for sample.txt that looks similar to this:

total 0
-rw-r--r-- 1 labex labex 0 Oct 26 10:30 sample.txt

Let's break down what each part of this line means:

  • -rw-r--r--: This represents the file's permissions. The first character (-) indicates it's a regular file. The following characters define who can read, write, or execute the file.
  • 1: The number of hard links to the file.
  • labex: The user who owns the file.
  • labex: The group that owns the file.
  • 0: The size of the file in bytes. Since we created it with touch, it's empty.
  • Oct 26 10:30: The date and time the file was last modified.
  • sample.txt: The name of the file.

Just like with the -a option, you can combine -l with it to see detailed information for all files, including hidden ones. The options can be written as ls -l -a, ls -la, or ls -al. They all do the same thing.

Let's try it. Execute the command to see a long listing of all files:

ls -la

Now the output includes the hidden file and the special directories . and ..:

total 8
drwxr-xr-x 2 labex labex 4096 Oct 26 10:35 .
drwxr-xr-x 3 labex labex 4096 Oct 26 10:20 ..
-rw-r--r-- 1 labex labex    0 Oct 26 10:30 .hidden_file
-rw-r--r-- 1 labex labex    0 Oct 26 10:30 sample.txt

Notice that for . and .., the permissions string starts with a d. This indicates that they are directories, not regular files. Using ls -la is one of the most common commands you will use to get a comprehensive overview of a directory's contents and their properties.

In this final step, you will learn to navigate using relative paths. Unlike an absolute path, which starts from the root (/), a relative path starts from your current working directory. This is often a quicker way to move around nearby directories. The most important tool for relative navigation is the special directory .., which always represents the parent directory (the directory one level above your current one).

First, ensure you are in the ~/project directory. You can verify this with pwd.

pwd

The output should be /home/labex/project.

Now, let's move up to the parent directory, which is /home/labex. To do this, use the cd command with .. as the argument:

cd ..

This command tells the shell to change to the parent of your current directory. Confirm your new location with pwd:

pwd

You will see that you are now in your home directory:

/home/labex

You can chain .. to move up multiple levels at once. For example, from /home/labex, running cd ../.. would take you to the root directory (/).

Finally, let's practice the quickest way to return to your home directory from anywhere on the system. As you learned in the first step, simply executing the cd command without any arguments will always take you back to your personal home directory.

Let's move to the /etc directory first:

cd /etc

Now, from /etc, return to your home directory with a single command:

cd

Verify your location one last time with pwd.

pwd

The output confirms you are back home:

/home/labex

Congratulations! You have now practiced the most essential shell commands for navigating the Linux filesystem: pwd, cd with absolute and relative paths, and ls to view directory contents.

Summary

In this lab, you learned the fundamental shell commands for navigating the Linux filesystem. You practiced using pwd to identify your current working directory and cd to change directories. This included moving between locations using absolute paths, relative paths such as .. to go to a parent directory, and convenient shortcuts like the tilde (~) for your home directory. You also saw how executing cd without any arguments is a quick way to return home.

Additionally, you explored how to list and inspect directory contents with the ls command. You used the -a flag to view all files, including hidden ones, and the -l flag to display a detailed, long-format listing. This detailed view provided critical information about file properties, such as permissions, ownership, size, and modification dates, enhancing your ability to manage files effectively.