Readers like you help support MUO. When you make a purchase using links on our site, we may earn an affiliate commission. Read More.

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] <directory name> 

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 
Linux terminal commands showing the use of the mkdir command

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 <directory name1><directory name2><directory name3>....<directory name_n> 

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.

Ubuntu terminal window with code snippets to create multiple 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.

Linux Ubuntu desktop drop down menu

Once you see a new folder icon, you can rename the folder to a name of your choice.

New folder icon with a naming option on Ubuntu desktop

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 
Ubuntu terminal window with code snippets to crate a parent directory

2. -v or -verbose: The -v command adds verbose messages to each directory created.

 mkdir - v file1 file2 
Terminal command to add verbose to a directory

3. --version: The --version commands print the version details of the license.

 mkdir --version 
Ubuntu terminal to check Linux directory 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 
Terminal code snippets showing how to list the directory's contents in Ubuntu

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 <directory name> 

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:

  1. -f: Forcefully remove the files and folders from the directory
  2. -i: Asks for permissions before deleting a directory
  3. -r: Removes directories and the stored files recursively
  4. -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 
 Ubuntu terminal window with code snippets to remove multiple directories

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 <directory name> 

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.

rmdir error message within the Ubuntu terminal

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.