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.

Linux terminal commands showing the use of the mkdir command

How to Create a Directory With the Command Line

Themkdircommand holds the key to all your directory creation tasks. you may create single and multiple directories within the terminal with one line of code. Here’s how themkdircommand on Linux works:

1. Create a Single Directory on Linux

Here’s how you’re able to use themkdirfunction to create a single directory:

For example, if you want to create a new directory, you may use the following command:

Ubuntu terminal window with code snippets to create multiple directories

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 thecdcommand:

The cd command is a highly flexible way to change directorieswithin the Linux terminal.

Linux Ubuntu desktop drop down menu

2. Create Multiple Directories on Linux

Themkdirfunction 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:

To create three folders, here’s how you can use themkdircommand:

Once you run the above command, the program creates three directories: MUO_sample1, MUO_sample2, and MUO_sample3. You can run thelscommand 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.

New folder icon with a naming option on Ubuntu desktop

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 themkdirfunction 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.

Ubuntu terminal window with code snippets to crate a parent directory

From the list menu, click on theNew Folderoption.

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 thecdcommand to navigate to the specific location and run thelscommand to list the contents.

mkdir Options

You can use a few options with themkdircommand, which helps you enhance the existing utility of the command. You can use the commands listed below:

1.-p or -parents: The-pcommand is helpful when creating a parent directory and subdirectory together. If the specified parent directory exists already, it automatically creates the subdirectory.

  1. -v or -verbose: The-vcommand adds verbose messages to each directory created.

3. –version: The–versioncommands print the version details of the license.

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:

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 thermcommand 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.

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 theuse the ls command to list the directory’s contents.

The command lists all the directory’s contents. You will see a list of all the files stored within this directory on your screen.

  1. Delete a Directory via the Terminal

Type in thermcommand, followed by the different commands and the directory name you want to delete.

you may use thelscommand again to confirm the directory is no longer available.

Here are some of the most commonly used options with thermcommand:

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:

There is an alternate method you may use to delete empty directories. You can use thermdircommand to remove the directory quickly, as follows:

To remove the MUO3 directory when it does not have any files/subdirectories in it, you can use thermdircommand as below:

However, if the directory isn’t empty, you will get an error message whenever you try to delete the directory with thermdircommand.

Use thermandrmdircommands carefully, as the deleted directories can’t be retrieved.

4. Delete a Directory Directly

The way you create a directory, you’re able to even delete the directory/folder directly. Select the directory you want to delete and press theDeletebutton 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.