Creating Directories Using Command Line in Linux
Linux is Unix-based and Unix was originally designed to provide a stable, reliable, and powerful environment, but easy to use. And in Linux, one of the important things is the command Line. One of the main reasons to use Linux is to use the command line. Because it’s so powerful.
How to make Directories using command Line?
We can make directories by using the command mkdir means “make Directory”. First, you go to the Directory where you want to create a Directory. In my case, the name of the Directory will be Grades. So, you can imagine the pattern which is mkdir [Directory-Name]
mkdir Grades
It creates a directory with the name of Grades. Now, we have to test that is this directory exists? By using this command.
test -d "Grades" && echo "Exists" || echo "Does not exists"
How to create Hierarchy of Directories?
Now, we want to create a tree of Directories by using the command line. So, we will use this command.
mkdir -p Grades/{A,B,C}
In this command -p means Parent Directory. It will make Grades as a Parent Directory and A, B, and C as a subdirectory in the Grades Directory. By the ‘ls’ command you can see the subdirectories within directories.
So, you can now imagine the pattern that if you want to create another directory in these subdirectories then go to that particular directory and make that directory parent and then the same procedure as we discussed above.
Now, if you want to create a single directory within a Directory.
makdir Grades/D
How to create files using command Line?
Now, we are going to create files in directories by command Line.
cat > hello.txt
This command will create a hello.txt file in the current working directory.
Press Control+d command after content writing.
Now the problem with this command is that it if you have already some data in the file. Then, it is going to overwrite it. So, to avoid this problem. You may use the command.
cat >> hello.txt
It will append the data instead of overwriting it.
Now, we can read the data of the file in the terminal by the following command.
cat [file-Name]
So, now you can create multiple files and multiple directories using the command line. There is another command to create a file and the command is
touch [file-name]
Visualization of the Hierarchy of Directories
We can visualize the Hierarchy of Directories by a command that is the tree. First of all, we need to install it. So, to install it run the command.
sudo apt-get install tree
Now, as we have installed it. Let’s go and use it. So, to show the tree run the command.
tree [Directory-Name]
Make sure you are in that directory where your Directory exists.
How to remove Directories/files using command Line?
Now, we are going to learn how to remove files or directories using the command Line. So, if you want to remove a single file.
rm [file-name]
Now, if we want to remove the Directory. We can use this command.
rmdir [Directory-Name]
We can also delete the whole directory, its subdirectories, and files in it by a single command.
rm -r [Directory-Name]
In my case, I have to remove the Directory name Grades.
You can also remove a specific directory within a directory by using the following command.
rm -rv a/b/c
This command will delete the c directory and file in the c directory as well i.e it will not delete a and b directory.