Welcome! In this lesson, we will explore essential Directory Operations using Bash. Directories are a fundamental part of any operating system, and knowing how to manage them efficiently is crucial for any developer or system administrator. Directory operations are tasks performed on directories, such as creating, listing, navigating, and deleting them.
Let's dive into the details and get you comfortable with directory operations in Bash!
Before we delve into directory operations, let's understand what directories are. Directories, also known as folders, are special types of files that serve as containers to organize and store other files and directories. They help maintain a structured and hierarchical file system, making it easier to manage and locate data.
In a Unix-like operating system, the file system is structured as a tree, where directories can contain files and subdirectories. The root directory / is at the top of this tree, and every other directory or file falls within this hierarchical structure.
A directory structure diagram is a visual representation of the hierarchical organization of files and directories within a file system. It illustrates the relationship between directories (also known as folders) and files, showcasing how they are nested within each other.
Let's break down the following diagram:
/: The root directory, which is the top-level directory in the file system.home: A subdirectory within the root directory/. This is often used to store user directories. The * indicateshomeis the current working directory.documents: A subdirectory within thehomedirectory.doc.txt: A file located within thedocumentsdirectory.
projects: Another subdirectory within thehomedirectory.p1.txt: A file located within theprojectsdirectory.
In Unix-like operating systems, file paths specify the location of files and directories within the file system. They can be absolute or relative, and understanding the difference is essential for effectively navigating and managing files.
An absolute path provides the complete address to a file or directory, starting from the root directory (/). It always begins with a / and specifies the entire directory hierarchy needed to reach the file or directory.
For example, /home/projects/p1.txt is an absolute path. No matter your current directory, using this path will always lead to p1.txt.
A relative path specifies the location of a file or directory in relation to the current working directory. It does not start with / and is usually shorter than an absolute path.
If your current working directory is /home, the relative path projects/p1.txt will lead you to the same p1.txt as the absolute path in the previous example.
The current working directory (CWD) is a term used in Unix-like operating systems to denote the directory in which a user or a process is presently operating. It's essentially the folder you are "in" at any given time while using the command line interface.
The command to print the CWD is:
When you first open the CodeSignal IDE, the command pwd prints /usercode/FILESYSTEM because this is the default working directory in the CodeSignal environment where your code is executed.
The ls command in Bash is used to list the files and directories within a specified directory. Running ls without a path lists all files and directories in the current working directory. ls can also accept a path to a directory. Let's take a look:
Suppose we have the following directory structure:
If our CWD is home, we can add a file and use the ls command as follows
After creating file.txt, our directory structure is:
The output of the ls command is:
You can also pass in a path to ls. For example:
This command will print:
Now that we understand what directories are, let's move on to creating and navigating directories.
mkdir, short for "make directory" creates a new directory inside the current working directory. mkdir can also accept a path to a directory. Let's take a look:
This command creates a new directory named projects inside the current working directory. Our new directory structure is:
Running ls will output:
This shows that the projects directory was successfully added to the /home directory.
You can also pass in a path to mkdir. For example:
The mkdir command navigates to the /home/projects directory and creates a new directory called project1
Our new directory structure is:
Running ls projects results in:
To delete a directory in Bash, you can use either the rmdir or rm -r command depending on whether the directory is empty or not. rmdir will only remove a directory if it does not contain any files or subdirectories. Using rmdir on a non-empty directory results in an error.
Recall that we created an empty project1 directory inside /home/projects. We can remove the directory as follows:
rmdirdeletes the emptyproject1directory.- The
lscommand does not return anything because theprojectsdirectory is now empty.
Our new directory structure is now:
The rm -r command recursively removes the directory and all of its contents, including any files and subdirectories. Let's create a new directory called project2 inside our projects directory. We will then attempt to delete the projects directory. Let's take a look:
The cd command, short for "change directory" allows you to change the current working directory. After executing this command, all subsequent commands will be executed from within the new current working directory. Suppose our CWD is /home.
For example:
mkdir projectscreates a new directory namedprojectsin the current working directory.cd projectschanges the current working directory to the newly createdprojectsdirectory. After executing this command, all subsequent commands will be executed from within theprojectsdirectory.
The output of the code is:
This output shows that the new current working directory has changed to the projects directory.
There are special characters and notations used in file paths to simplify navigation and management:
.: Refers to the current directory...: Refers to the parent directory, one level up in the hierarchy.
The . in the context of the cd command represents the current directory. While it may seem redundant to use cd . (since it simply means "change to the current directory"), it's often used in more complex scripts or operations involving relative paths.
In Bash, .. represents the parent directory of the current working directory. It is used with the cd command to navigate up one level in the directory hierarchy.
Suppose we have the following directory structure:
Assume you are in the directory /home/projects and you want to move up to the /home directory.
- The first
pwdcommand shows the current working directory is/home/projects - The
cd ..command changes the current working directory to the parent directory. Since you are currently in , moving up one level will take you to .
Great job! Today, you've learned how to:
- Print the home and working directories.
- Create and navigate directories.
- List files in a directory.
- Copy and move directories.
- Clean up by removing directories and files.
These skills are essential for managing your file system efficiently. Now, it's time to solidify your understanding by practicing these operations. Dive into the exercises to strengthen your skills and see these commands in action. Happy scripting!
