Navigating The Linux File System

Introduction

Welcome to Chapter 13 of our "Mastering AWS DevOps" series! In this blog post, we'll delve into the essentials of navigating the Linux file system, an indispensable skill for any DevOps professional. Understanding how Linux organizes and manages data is crucial for efficiently working within a Linux environment.

Imagine a busy street in your hometown, filled with various shops and lanes. The Linux file system is quite similar, with its own set of paths and directories. Knowing how to navigate this "street" effectively will make your journey through the Linux environment smooth and efficient.

In this blog, you will learn about:

Step-by-Step Guide

Absolute Paths:

Absolute path is like a main road that everyone knows. It did not matter where you are on the street, you will always use this main road to find any shop.

In Linux world, this is called a root directory, symbolized by a forward slash (/). It's your starting point for any journey within the system.

Relative Paths:

Now, let’s think that you find a few shortcuts and side lanes to reach a shop without going back to the main road. These are the relative paths in Linux.

They start from your current location (the working directory) and help you navigate without always going back to the main road (root directory).

All the shops, lanes, and everything else on this busy street are organized under the main road, just like how Linux organizes its files and directories under the root directory (/). This structure is like a map of the street, maintained by the Linux Foundation.

And guess what? You won't have to wander around, lost and confused. We have fundamental commands in Linux that will serve as your guidebook. These commands are the essence of navigating this intricate street, letting us move from shop to shop, explore lanes, and find exactly what we need.

So, grab your virtual walking shoes, and let's explore the busy street of the Linux file system together! Shall we begin?

Let’s do it.

The first command is the cd (Change Directory) command.

The cd command is a fundamental command used to change the current working directory in Linux, Unix, and Windows operating systems using the terminal.

It's as simple as "cd [directory_path]" to switch to a new location. Let's put theory into practice.

        Syntax: cd [directory_path]
$ cd /home/username/documents
    

To navigate to the parent directory. This will change the current working directory to the parent directory of the current directory.

        $ cd ..
    

To navigate to the home directory. This will change the current working directory to the home directory of the current user.

        $ cd ~
    

To navigate to the previous directory. This will change the current working directory to the previous directory.

        $ cd -
    

To navigate to a directory located two levels above the current directory in the file system hierarchy.

        $ cd ../../parent_directory
    

Let's break down the command step by step.

Let’s understand this with an example.

Suppose you are currently in the directory /home/user/documents/reports/projectA/subfolder, and you want to move to the directory /home/user. By using the cd ../../.. command, you can achieve this in a concise manner.

Here's the breakdown.

After executing the command, you would be in the /home/user directory, which is your target directory.

In summary, the command cd ../../.. allows you to navigate upward in the directory structure by moving up multiple levels. Each ../ represents a step upward, and the number of ../ segments determines how many levels you move up. This can be a useful way to quickly jump to a higher-level directory without having to type out the complete path.

2. ls (List):

Now that we're in a new territory, let's use "ls" to see what's around us. With options like "-l" for a detailed list and "-a" to reveal hidden files, we gain insight into our surroundings. It displays the contents of the current directory.

        Syntax: ls [options] [directory]

To list the contents of the current directory. This will list all files and directories in the current directory.

$ ls

To list the contents of a specific directory. This will list all files and directories in the Documents directory.

        $ ls /home/user/Documents/
    

To display file size in easy-to-read format, use the -h flag. This will display the file size in human-readable format, such as M for MB, K for KB, G for GB.

        $ ls -lh
    

To display the file permissions, owner, group, and modification time, use the -l flag. This will display the detailed information about files and directories.

        $ ls -l
    

To display all files, including hidden files, use the -a flag. This will display all files and directories, including hidden files.

        $ ls -a
    

Combine two flags.

        $ ls -la
    
        OUTPUT
total 80
drwxr-xr-x 2 user user 4096 Aug 16 10:30 .
drwxr-xr-x 20 user user 4096 Aug 16 10:30 ..
-rw-r--r-- 1 user user 6148 Aug 16 10:30 file1.txt
-rw-r--r-- 1 user user 4096 Aug 16 10:30 file2.txt
drwxr-xr-x 2 user user 4096 Aug 16 10:30 folder1
drwxr-xr-x 2 user user 4096 Aug 16 10:30 folder2
    

The first column shows the permissions for each file or directory...

3. pwd (Print Working Directory):

Whenever you need to pinpoint your location, "pwd" comes to the rescue. This command is useful when you want to know where you are in the file system hierarchy.

        Syntax: pwd
$ pwd
/home/username/documents
    

To resolve symbolic links and print the path of the target directory, use the -L flag. This will print the symbolic path of the current working directory.

        $ pwd -L
    

To display the actual path without resolving symbolic links, use the -P flag. This will print the actual path of the current working directory.

        $ pwd -P
    

In summary, the pwd command in Linux is used to print the current working directory...

4. mkdir (Make Directory):

To create a new path on our journey, we use "mkdir." It allows you to create a new directory. This command is useful when you want to create a new directory to store files.

        Syntax: mkdir [directory_name]
$ mkdir test
    

To create multiple directories at once, use the -p flag. This will create three directories named "dir1", "dir2", and "dir3" in a nested structure.

        $ mkdir -p dir1/dir2/dir3
    

To create a directory with specific permissions, use the -m flag. This will create a directory named "my_dir" with read, write, and execute permissions for all users.

        $ mkdir -m 777 my_dir
    

To create a directory with a specific parent directory, use the -p flag. This will create a directory named "child_dir" inside a directory named "parent_dir".

        $ mkdir -p parent_dir/child_dir
    

To create a directory with a specific owner and group, use the -o and -g flags. This will create a directory named "my_dir" with the specified owner and group.

        $ mkdir -o user -g group my_dir
    

In summary, the mkdir command in Linux is used to create new directories in the Linux file system. It has various command line options that can be used to modify its behavior. It is important to use the mkdir command with caution, as it can create directories with specific permissions, owners, and groups.

5. rmdir (Remove Directory):

When it's time to bid farewell to an empty directory, "rmdir" will do the trick. It allows you to remove a directory. This command is useful when you want to delete a directory that is no longer needed.

        Syntax: rmdir [directory_name]
$ rmdir test
    

To remove an empty directory. This will remove the specified empty directory from the file system.

        $ rmdir directory_name
    

To remove multiple empty directories at once, specify the names of the directories separated by spaces. This will remove the specified empty directories from the file system.

        $ rmdir dir1 dir2 dir3
    

To remove a directory and its parent directories if they are empty, use the -p flag. This will remove the specified directory and its parent directories if they are empty.

        $ rmdir -p directory/subdirectory
    

To display verbose information for each directory that is removed, use the -v flag. This will display a message for each directory that is removed.

        $ rmdir -v directory_name
    

To ignore the failure if the directory is not empty, use the --ignore-fail-on-non-empty flag. This will suppress the warning and remove the directory even if it is not empty.

        $ rmdir --ignore-fail-on-non-empty directory_name
    

To force removal of a directory, use the -f flag. This will remove the specified directory without prompting for confirmation, even if it is not empty.

        $ rmdir -f directory_name
    

In summary, the rmdir command in Linux is used to remove empty directories from the file system. It has various command line options that can be used to modify its behavior, such as removing directories and their contents, forcing removal, displaying verbose information, removing parent directories if they are empty, and ignoring failures.

6. cp (Copy) and mv (Move):

These twin commands allow us to either clone files or move them to a new destination.

The cp command stands for "copy." It allows you to copy files or directories from one location to another. This command is useful when you want to make a backup of important files.

        Syntax: cp [options] [source] [destination]
$ cp Documents/file.txt Downloads/
    

To copy a file from one location to another, use the following command. This will copy the file file.txt to the Documents directory.

        $ cp file.txt /home/user/Documents/
    

To copy a directory and its contents, use the -r flag. This will copy the directory dir1 and all its contents to the Documents directory.

        $ cp -r dir1 /home/user/Documents/
    

To preserve the original file attributes, such as ownership and permissions, use the -p flag. This will copy the file file.txt to the Documents directory while preserving its original attributes.

        $ cp -p file.txt /home/user/Documents/
    

To display verbose information for each file that is copied, use the -v flag. This will display a message for each file that is copied.

        $ cp -v file.txt /home/user/Documents/
    

To force overwriting of the destination file, use the -f flag. This will overwrite the destination file if it already exists.

        $ cp -f file.txt /home/user/Documents/
    

To prompt for confirmation before overwriting the destination file, use the -i flag. This will prompt you for confirmation before overwriting the destination file.

        $ cp -i file.txt /home/user/Documents/
    

In summary, the cp command in Linux is used to copy files and directories from one location to another. It has various command line options that can be used to modify its behavior, such as copying directories and their contents, preserving original file attributes, displaying verbose information, overwriting destination files, and prompting for confirmation before overwriting.

The mv command stands for "move." It allows you to move files or directories from one location to another. This command is useful when you want to organize files into different directories.

        Syntax: mv [options] [source] [destination]
$ mv Documents/file.txt Downloads/
    

To move a file to a different directory. This will move the file file.txt to the Documents directory.

        $ mv file.txt /home/user/Documents/
    

To rename a file. This will rename the file old_name.txt to new_name.txt.

        $ mv old_name.txt new_name.txt
    

To move multiple files to a directory. This will move both file1.txt and file2.txt to the Documents directory.

        $ mv file1.txt file2.txt /home/user/Documents/
    

To force overwrite an existing file in the destination directory, use the -f flag. This will overwrite the file if it already exists in the destination directory.

 $ mv -f file.txt /home/user/Documents/
    

To prompt for confirmation before overwriting an existing file, use the -i flag. This will prompt you for confirmation before overwriting the file.

        $ mv -i file.txt /home/user/Documents/
    

In summary, the mv command in Linux is used to rename and move files and directories. It has various command line options that can be used to modify its behavior, such as moving files to different directories, renaming files, overwriting existing files, and prompting for confirmation.

7. rm (Remove):

The rm command stands for "remove." It allows you to delete files or directories. This command is useful when you want to delete files or directories that are no longer needed.

        Syntax: rm [options] file(s)
$ rm Downloads/file.txt
    

Here, [options] are optional flags that modify the behavior of the rm command, and file(s) represents the name of the file(s) you want to remove.

For example, if you want to remove a file named "example.txt," the command would be:

        $ rm example.txt
    

If you want to remove multiple files, you can list them all separated by spaces:

        $ rm file1.txt file2.txt file3.txt
    

You can also use wildcards to specify files with a certain pattern. For instance, to remove all files with a .log extension, you will use.

        $ rm *.log
    

To remove a directory and its contents, use the -r flag.

        $ rm -r directory
    

To remove a file interactively, use the -i flag. This will prompt you for confirmation before removing the file.

        $ rm -i file.txt
    

To remove a file without prompting for confirmation, use the -f flag.

        $ rm -f file.txt
    

To remove all files in a directory, use the wildcard character *. This will remove all files in the current directory, but not subdirectories.

        $ rm *
    

To remove all files in a directory and its subdirectories, use the -r and -f flags together. This will remove the directory and all its contents without prompting for confirmation.

        $ rm -rf directory
    

Commonly Used Options:

What is the difference between rmdir and rm command?

Do you get a feeling that rmdir and rm sound similar? Well, the rmdir and rm commands in Linux are used to remove directories, but they have some differences.

Let’s see what they are.

rmdir Command:

The rmdir command is specifically designed to remove empty directories (folders). It cannot be used to remove directories that contain files or other subdirectories.

It is a safer option when you only want to remove an empty directory and avoid accidentally deleting files.

rm Command:

The rm command is a more versatile command that can be used to remove both files and directories, whether they are empty or not. When removing a directory using rm, you need to use the -r (or -R) flag to perform a recursive removal, which includes removing all files and subdirectories within the specified directory.

It is more powerful but also potentially more dangerous, as it can lead to unintended data loss if not used carefully.

In summary, if you need to remove an empty directory, you should use the rmdir command. However, if you want to remove directories (whether empty or not) along with their contents, the rm command with the -r (or -R) flag is the appropriate choice. Always exercise caution when using the rm command, especially with the -r flag, as it can result in irreversible data loss.

A Summary and Next Steps:

And there you have it – a whirlwind tour of essential Linux navigating commands! Armed with these tools, you've acquired the skills to navigate the Linux file system hierarchy like a pro. Remember, practice makes perfect, so don't hesitate to embark on your own explorations.

In this technical blog, we have explained all the essential Linux file navigation commands that can help you navigate through the Linux file system hierarchy. With these foundational commands, you've unlocked the art of Linux navigation. "cd," "ls," "pwd," "mkdir," "rmdir," "cp," and "mv" have become your allies, paving the way for efficient exploration.

I suggest a few further actions like experimenting with the commands in your own Linux environment, exploring advanced options and flags for each command, learning about wildcard characters (*, ?, []) to enhance your command usage and familiarizing yourself with keyboard shortcuts for faster navigation.

Conclusion:

Linux's filesystem hierarchy is no longer a mystery. Armed with these commands, you're poised to journey through its intricate paths. So go ahead, explore fearlessly, and unravel the wonders of Linux's digital landscape!

Share this post:

Back