Directories are essential to your Linux OS, as they contain files you might use regularly or your system needs to function correctly. While there is always an option to create folders manually using a file manager, many advanced users might prefer using a terminal window to create and navigate through files and folders on their machines.
If you are a Linux aficionado and want to master creating and managing directories from the command line, here's what you need to know.
How to Create a Directory With the Command Line
The mkdir command holds the key to all your directory creation tasks. You can create single and multiple directories within the terminal with one line of code. Here's how the mkdir command on Linux works:
1. Create a Single Directory on Linux
Here's how you can use the mkdir function to create a single directory:
mkdir [option]
For example, if you want to create a new directory, you can use the following command:
mkdir MUO
The above command creates a new MUO directory within the current location. To navigate to the MUO directory via the command line, you can then use the cd command:
cd ./MUO
The cd command is a highly flexible way to change directories within the Linux terminal.
2. Create Multiple Directories on Linux
The mkdir function is versatile, allowing you to create multiple directories with a single command. For example, to create multiple folders via the command line, use the code below:
mkdir ....
To create three folders, here's how you can use the mkdir command:
mkdir MUO_sample1 MUO_sample2 MUO_sample3
Once you run the above command, the program creates three directories: MUO_sample1, MUO_sample2, and MUO_sample3. You can run the ls command to list the newly created directories.
The Linux directory structure is intricate, so it's always best to familiarize yourself with parent and child directories to avoid unnecessary roadblocks.
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:
1. -p or -parents: The -p command is helpful when creating a parent directory and subdirectory together. If the specified parent directory exists already, it automatically creates the subdirectory.
mkdir -p MUO/MUO_CHILD
2. -v or -verbose: The -v command adds verbose messages to each directory created.
mkdir - v file1 file2
3. --version: The --version commands print the version details of the license.
mkdir --version
Delete Empty and Non-Empty Linux Directories
Finally, once you have created directories on your Linux OS, you might want to remove the ones you no longer need. You can delete the directories on your Linux OS in two ways:
- rm
- rmdir
Let's discuss both these methods in detail:
1. Using the rm Command
If you want to delete empty and non-empty directories, you can use the rm command as follows:
1. Check the Directory’s Contents
Before deleting the directory, you should check its contents. To do so, navigate to the directory's location with the cd command.
cd ./MUO
In the example above, MUO is a directory on the Ubuntu Desktop.
2. List the Directory’s Contents
Once you are in the directory's location, use the use the ls command to list the directory's contents.
ls
The command lists all the directory's contents. You will see a list of all the files stored within this directory on your screen.
3. Delete a Directory via the Terminal
Type in the rm command, followed by the different commands and the directory name you want to delete.
rm -option
You can use the ls command again to confirm the directory is no longer available.
Here are some of the most commonly used options with the rm command:
-f: Forcefully remove the files and folders from the directory
-i: Asks for permissions before deleting a directory
-r: Removes directories and the stored files recursively
-d: Removes an empty directory
You can even remove multiple directories in one go. For example, when you want to remove directories MUO_sample1, MUO_sample2, and MUO_sample3 with one command, here's how you can do it:
rm -d MUO_sample1 MUO_sample2 MUO_sample3
There is an alternate method you can use to delete empty directories. You can use the rmdir command to remove the directory quickly, as follows:
rmdir
To remove the MUO3 directory when it does not have any files/subdirectories in it, you can use the rmdir command as below:
rmdir MUO3
However, if the directory isn't empty, you will get an error message whenever you try to delete the directory with the rmdir command.
Use the rm and rmdir commands carefully, as the deleted directories can't be retrieved.
4. Delete a Directory Directly
The way you create a directory, you can even delete the directory/folder directly. Select the directory you want to delete and press the Delete button on your keyboard. Ensure you check the directory's contents before deleting it, as the effect is permanent via this method.
Working With Linux Directories Effortlessly
Directories form a significant part of your files and folder structures within Linux. Like any other OS, creating and deleting directories is fairly easy on Linux, especially when you want to work seamlessly from within the terminal window.
To make the most of your experience, you can use the above commands, and in a few easy steps, you can quickly achieve the task at hand.
Recommended Comments
There are no comments to display.
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.