How to create and delete folders in Linux Terminal

Folders are essential for your Linux operating system because they contain files you may use often or that the system needs to function correctly.

While there is always the option of creating folders manually using a file manager, many advanced users may prefer to use a terminal window to create and navigate through files and folders on their machine.

If you're a Linux enthusiast and want to master creating and managing folders from the command line, here's what you need to know.

How to create folders using command line

The mkdir command holds the key to all your directory creation tasks. You can create one or more folders in 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 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 folder in the current location. To navigate to the MUO directory via the command line, you can use the cd command:

cd ./MUO

How to create and delete folders in Linux Terminal Picture 1How to create and delete folders in Linux Terminal Picture 1

 

The cd command is a very flexible way to change directories in the Linux terminal.

2. Create multiple folders on Linux

The mkdir function is very flexible, allowing you to create multiple directories with just one command. For example, to create multiple folders via command line, use the code below:

mkdir .

To create 3 directories, here is how you can use the mkdir command:

mkdir MUO_sample1 MUO_sample2 MUO_sample3

When you run the above command, the program will create 3 folders: MUO_sample1, MUO_sample2 and MUO_sample3. You can run the ls command to list newly created directories.

How to create and delete folders in Linux Terminal Picture 2How to create and delete folders in Linux Terminal Picture 2

The Linux directory structure is complex, so it's best to familiarize yourself with parent and child directories to avoid unnecessary hurdles.

 

3. Create folders directly

As a Linux user, you can create folders directly at the desired location. You do not need to navigate to the location or use the mkdir function in the terminal.

All you need to do is go to the folder/location where you want to create a new folder/folder and right click.

From the list menu, click on the New Folder option .

How to create and delete folders in Linux Terminal Picture 3How to create and delete folders in Linux Terminal Picture 3

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

How to create and delete folders in Linux Terminal Picture 4How to create and delete folders in Linux Terminal Picture 4

 

Rest assured, if you want to check the directory status from a terminal window, you can still use the cd command to navigate to a specific location and run the ls command to list the contents.

mkdir options

You can use several options with the mkdir command, which help you enhance the command's existing utility. You can use the commands listed below:

1. -p or -parents : The -p command is useful when creating parent folders and child folders together. If the specified parent folder already exists, it will automatically create subfolders.

mkdir -p MUO/MUO_CHILD

How to create and delete folders in Linux Terminal Picture 5How to create and delete folders in Linux Terminal Picture 5

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

mkdir - v file1 file2

How to create and delete folders in Linux Terminal Picture 6How to create and delete folders in Linux Terminal Picture 6

3. --version : The --version command outputs license version details.

 

mkdir --version

How to create and delete folders in Linux Terminal Picture 7How to create and delete folders in Linux Terminal Picture 7

Delete empty and non-empty Linux folders

Finally, once you have created folders on your Linux operating system, you may want to delete those you no longer need. You can delete folders on your Linux operating system in two ways:

  1. rm
  2. rmdir

Let's discuss both these methods in detail!

If you want to delete empty and non-empty folders, you can use the rm command as follows:

1. Check the contents of the folder

Before deleting a folder, you should check its contents. To do so, navigate to the folder's location using the cd command.

cd ./MUO

In the example above, MUO is a folder on the Ubuntu Desktop.

2. List directory contents

Once you are in the directory's location, use the ls command to list the directory's contents.

ls

How to create and delete folders in Linux Terminal Picture 8How to create and delete folders in Linux Terminal Picture 8

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

3. Delete the folder via Terminal

Enter the command rm, followed by the various commands and the name of the folder you want to delete.

rm -option 

You can use the ls command again to confirm the directory is no longer there.

Here are some of the most commonly used options with the rm command:

  1. -f: Force deletion of files and folders from the directory
  2. -i: Request permissions before deleting folders
  3. -r: Delete stored directories and files recursively
  4. -d: Delete empty directory

You can even remove multiple folders in one go. For example, when you want to delete the folders 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

How to create and delete folders in Linux Terminal Picture 9How to create and delete folders in Linux Terminal Picture 9

 

There is an alternative method you can use to delete empty folders. You can use the rmdir command to quickly delete directories as follows:

rmdir 

To delete the MUO3 folder when it does not have any files/subfolders in it, you can use the rmdir command as shown below:

rmdir MUO3

However, if the directory is not empty, you will receive an error message whenever you try to delete the directory using the rmdir command.

How to create and delete folders in Linux Terminal Picture 10How to create and delete folders in Linux Terminal Picture 10

Note : Use rm and rmdir commands with care because deleted directories cannot be retrieved.

4. Delete folder directly

According to the way you create the folder, you can even delete that folder/folder directly. Select the folder you want to delete and press the Delete button on the keyboard. Make sure you check the folder's contents before deleting it, as this method is permanent.

5 ★ | 1 Vote