Creating and Deleting Directories in Linux Terminal

The Linux command line provides immense power and flexibility for system administration tasks. Creating and managing directories is one of the most common day-to-day activities for Linux users. Mastering directory operations via the terminal accelerates your workflow and unlocks advanced capabilities.
In this guide, we will cover how to efficiently create, navigate, and remove directories using Bash shell commands. Whether you are a programmer, DevOps engineer, or Linux enthusiast, these skills will prove invaluable.
Creating Directories in Linux Using the Terminal
The mkdir command allows creating new directories from the terminal. Let's explore the different ways to use it.
Creating a Single Directory
To create a directory, simply specify the name after mkdir:
mkdir MyNewDir
This will create a directory called MyNewDir in the current working directory.
You can verify it was created using ls:
ls
MyNewDir
Creating Multiple Directories
You can create multiple directories by passing multiple names to mkdir:
mkdir Dir1 Dir2 Dir3
This efficiently creates three directories - Dir1, Dir2 and Dir3.
Creating Nested Directories
To create nested directories, i.e. a directory within another directory, use the -p option:
mkdir -p Parent/Child/Grandchild
This will create the Parent, Child and Grandchild directories. -p ensures the parent directories are created if needed.
Creating Directories in Specific Location
By default, mkdir creates directories in the current working directory. To explicitly define the path, provide it before the directory name:
mkdir /home/user/MyDir
This will create MyDir under /home/user/ regardless of the current location. Absolute paths help create directories precisely where needed.
Navigating Directories in Linux Terminal
Once directories are created, you'll want to navigate between them. The cd command handles this.
Navigating to Home Directory
To jump to home directory, use:
cd ~
or
cd
This is handy when you need to quickly switch to your user's home folder.
Navigating to Absolute Path
To navigate to a directory via absolute path:
cd /home/user/MyDir
This moves you precisely to /home/user/MyDir.
Navigating to Relative Path
You can use relative paths for moving between directories:
cd ../Parent
The ../ goes up one level to the parent directory. You can chain multiple, like ../../Grandparent to go up two levels.
Navigating to Previous Directory
To return to previous directory:
cd -
This special case for cd jumps back to the last location. You can swap between two directories easily.
Completion and Listing Directories
When typing paths, press TAB to auto-complete directory names.
To view contents of a directory, use ls. It lists files and sub-directories.
These help navigate precisely.
Removing Directories in Linux Command Line
Once directories are no longer needed, you can delete them using:
rmdir for Empty Directories
rmdir deletes empty directories.
rmdir MyEmptyDir
Attempting to remove non-empty directories will result in an error.
rm -r for Non-Empty Directories
For directories containing files or other directories, use rm -r:
rm -r MyDirWithContents
This recursively deletes MyDirWithContents and all its contents.
Use with caution as data deletion is permanent.
Safely Deleting Directories Interactively
To enable prompts before deleting:
rm -r -i MyDir
The -i option will prompt before removing each file or directory. This prevents accidental data loss.
Removing Directory Tree Forcefully
If you are absolutely certain, deleting a directory tree forcibly skips confirmation prompts:
rm -rf MyDir
The -fforces removal without prompting. Use this only when absolutely needed.
Conclusion
The Linux terminal offers immense power for handling directories. Mastering the mkdir, cd, and rm commands allows you to efficiently create, navigate, and remove directories exactly as needed.
Whether it's setting up new projects, cleaning up old ones, or quickly accessing particular locations, these skills will boost your shell proficiency. With the ability to precisely manipulate directories, you can breeze through many administrative and development tasks on Linux.






