Creating and Removing Directories in Terminal
Directories are the building blocks of your Linux operating system. Whether you’re a seasoned Linux user or just starting your journey, understanding how to Creating and Removing directories using the command line is a valuable skill. In this comprehensive guide, we’ll walk you through the essential commands and strategies to effortlessly manage directories on your Linux machine.
How to Create a Directory With the Command Line
The Linux terminal is your gateway to efficient directory management. Let’s dive into the mkdir command, your key to creating directories:
1. Create a Single Directory on Linux
The basic syntax to create a single directory is as follows:
cd
command to navigate into the newly created directory.
2. Create Multiple Directories on Linux
The mkdir
command is versatile, allowing you to create multiple directories at once. Use this syntax:
This command will swiftly create three directories for you.
3. Create Directories Directly
As a Linux user, you can create a directory directly at the desired location. You don’t need to navigate to the location or use the mkdir function within the terminal.
All you need to do is head over to the folder/location where you want to create a new directory/folder and right-click.
From the list menu, click on the New Folder option.
Once you see a new folder icon, you can rename the folder to a name of your choice.
Rest assured, if you want to check the directory status from the terminal window, you can still use the cd command to navigate to the specific location and run the ls command to list the contents.
mkdir
Options
You can use a few options with the mkdir command, which helps you enhance the existing utility of the command. You can use the commands listed below:
-p
or-parents
: Create a parent directory and subdirectory together.-v
or-verbose
: Add verbose messages for each directory created.--version
: Check the version details of themkdir
command.
Deleting Empty and Non-Empty Linux Directories
Now that you know how to create directories, it’s time to learn how to remove them. Linux provides two primary methods: rm
and rmdir
.
Using the rm
Command
1. Check the Directory’s Contents
Before deleting a directory with rm
, inspect its contents with the cd
and ls
commands:
This ensures you won’t accidentally delete important files.
2. Delete a Directory via the Terminal
To remove a directory, use the rm
command followed by options and the directory name:
Common rm
options:
-f
: Forcefully remove files and folders.-i
: Prompt for permissions before deleting.-r
: Recursively remove directories and their contents.-d
: Remove an empty directory.
You can delete multiple directories at once:
Using the rmdir
Command
Use the rmdir
command to quickly delete empty directories:
However, if the directory isn’t empty, you’ll encounter an error.
Delete a Directory Directly
Just as you created directories directly, you can delete them directly. Select the directory, press the Delete button on your keyboard, and confirm. Remember to double-check the directory’s contents before doing this, as it’s irreversible.
Working With Linux Directories Effortlessly
Directories are the backbone of your Linux system. Mastering directory creation and deletion in the terminal empowers you to work seamlessly within Linux. Use these commands and techniques to streamline your Linux experience and efficiently manage your file structure.
Unlock the power of Linux terminal commands to create, manage, and remove directories effortlessly. Learn the tricks from a Linux pro!