Introduction To The Linux File System
Have you ever got lost while trying to find a file or folder on your computer? I know I have! I was working with Linux one day when my son stopped by and looked at my screen.
Are you playing a game? he asked, thinking that the Linux file system looked like some sort of puzzle.
His comment got me thinking. Navigating the Linux file system can feel like a puzzle or a maze, especially if you are new to it. Right?
But don’t worry! I want to help make it as simple and fun as finding your way around your own house. In this blog, I will show you the important commands you need to know as a Cloud Engineer.
You will learn how to find what you are looking for, and maybe even have some fun along the way. Excited? Let’s get started!
The Quest Begins: Essential Navigating Commands
Before we dive into the specifics…
Before we dive into the specifics of learning how to navigate the filesystem, it’s good to know how Linux organizes data.
Imagine a busy street in your hometown, and you need to find a particular shop. The Linux file system is a lot like that street, filled with many shops and lanes.
An absolute path is like a main road that everyone knows. It does not matter where you are on the street, you will always use this main road to find any shop.
In the Linux world, this is called a root directory, symbolized by a forward slash (
/). It’s your starting point for any journey within the system.
Now, let’s think you find a few shortcuts and side lanes to reach a shop without returning 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.
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.
cd:This is the command itself, which stands for “change directory.”
../:This notation represents moving up one level in the directory structure. It refers to the parent directory of the current directory.
../../:Here, we have two sets of
../, which means moving up two levels in the directory structure. It takes us to the grandparent directory of the current directory.
parent_directory:This is the name of the directory you want to navigate to. Replace it with the actual name of the directory you’re targeting.
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 concisely achieve this.
Here’s the breakdown.
- The first
../takes you up one level to
- The second
../takes you up another level to
- The third
../takes you up one final level to
After executing the command, you will 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.
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.
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 an 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, and 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…
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…
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
-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.
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 the 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.
cp (Copy) and
These twin commands allow us to either clone files or move them to a new destination.
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 the 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.
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
$ mv old_name.txt new_name.txt
To move multiple files to a directory. This will move both
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.
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
$ 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
$ 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
-f flags together. This will remove the directory and all its contents without prompting for confirmation.
$ rm -rf directory
Commonly Used Options:
-R: Recursively remove directories and their contents.
-f: Force removal without confirmation (use with caution).
-i: Prompt before every removal (safer, as it asks for confirmation).
-v: Verbose mode, showing each file as it’s being removed.
What is the difference between
Do you get a feeling that
rm sound similar? Well, the
rm commands in Linux are used to remove directories, but they have some differences.
Let’s see what they are.
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 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) 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) 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 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. “
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 familiarize yourself with keyboard shortcuts for faster navigation.
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!
Read it here: https://pravinmishra.in/launch-linux-instance-using-ec2-and-cloudformation-in-aws/