How to Create & Delete a Directory in Linux

admin-img By Manvinder Singh

Spread the love

how to create a directory in linux

Creating and deleting files and directories in Linux is one of the most fundamental tasks when working with the Linux command line. Whether you are managing and organizing project files, setting up a development environment, or managing system resources, knowing how to create and delete directories is vital for efficient file system navigation.

This guide will help you create and delete a directory on Linux. Let’s get started with how to create and delete a directory in Linux.

How to Create a Directory?

via the mkdir Command

The mkdir (make directory) command in Linux is a command-line utility that enables users to create new directories

Prerequisites

You need a Linux or UNIX-like system. Additionally, you will need access to a terminal or command line, as well as a user with permission to create and modify directory settings.

mkdir Command Syntax in Linux

The mkdir command has the following syntax:

mkdir [options] dir_name


Copied!

The [Options] section is optional and modifies the behavior of the mkdir command. The available options

The dir_name is the name or names of the directories you want to create. You can highlight multiple directory names separated by a space.

mkidr Linux Command Options

The options enable you to modify the mkdir command’s behavior. The table shows the most common mkdir options and their descriptions:

Option   Description  
-p  It is used to create parent directories if they don’t exist.  
-m a=[rwz]  [dir_name]  It sets the file modes, i.e, permissions for the new directory,  
-v or– verbose  It displays a message for each directory created. When utilized with the [directories] argument, it highlights the names of the directories being created.  
–version  It is used to display the version number and information about
-help   It sets the SELinux security context for directories.  
-Z  It is used to set the SELinux security context for directories.  

Creating a Directory in Linux

To create a directory using the terminal, pass the desired directory name to the mkdir command.

mkdir Linux


Copied!

If the operation is successful, the terminal returns an empty line. To verify the directory’s creation, use the Is command:
Picture

mkdir Linux

Creating a Directory in a Specific Location

To create directories in a particular location other than your current working directory. You need to specify the full directory path.

mkdir /tmp/example


Copied!

How to Create Multiple Directories?
With mkdir, it’s possible to create directories; it can be time-consuming when you need to make more than a few. To avoid that, run a single mkdir command and list the directory names separated by a space.

For example,

mkdir dir1 dir2 dir3


Copied!

The above command creates three particular directories. Also, you can use mkdir with curly brackets {} to create multiple directories. For instance, to create several directories, list them within curly brackets, without spaces:

mkdir {test1, test2, test3}


Copied!

You can also develop and create a batch of directories starting with the same pattern. Prepend the curly brackets with the pattern and use the brackets to specify the number of directories, for example;

mkdir dir{1..15}


Copied!

How to Delete a Directory in Linux?

When removing a directory in Linux, consider whether it is empty or contains files and other directories.

To delete a directory in Linux, there are two commands you can utilize: rmdir to delete empty directories and rm -r to remove directories with files or subdirectories.

Method 1: Use the ‘rmdir’ command- removing an empty directory.

To remove or delete a directory in Linux, you can use the rmdir command. This command is straight forward and safe because it only enables you to delete empty directories, preventing the accidental deletion of directories with content.

Syntax

rmdir [options] [directory name]


Copied!

Step 1: Check Your Current Directory

Firstly, open your Linux terminal. Now, before deleting the directory, use the ‘pwd’ command to determine where you are in the filesystem.

pwd


Copied!

Step 2: Remove the Directory

Now, to delete an existing empty directory, use the rmdir command:

rmdir gfg


Copied!

Step 3: Confirm the directory was deleted

Just write the command to be assured that the directory was deleted:

ls


Copied!

Method 2: Using the rm command to remove a directory

The rm command in Linux is a powerful utility used to delete files and directories. To remove a directory along with its contents, the -r (recursive) option is essential. It ensures that the entire directory and all its contents are permanently deleted.

Syntax:

rm [options] [file or directory name]


Copied!

Step 1: Navigate to Parent Directory

cd /home/kali/Documents/

rm -ri directory_name


Copied!

Step 2: Remove the Directory

To remove the directory and its contents, use the rm command with the –r (recursive) option.

rm -r GeeksforGeeks


Copied!

Step 3: Optional Confirmation

To confirm each deletion, you can add the ‘-I’ option to prompt for confirmation.

rm -ri directory_name


Copied!

By utilizing the rmdir command, we can easily delete a directory on our system. As you can see in the image, initially, when we created the directory, it was in our system. However, after the deletion, it was no longer visible in our file directories, and the file was successfully removed.

Summing Up

Managing directories is a fundamental skill for anyone working with the Linux command line. Whether you’re organizing project files or cleaning up your system, knowing how to create and delete directories efficiently can save you time and keep your workspace organized. With simple commands like mkdir for creating directories and rm -r or rmdir for deleting them, you have complete control over your file system. As always, exercise caution—especially when using rm -r—to avoid accidentally deleting essential data

autor-img

By Manvinder Singh

Manvinder Singh is the Founder and CEO of HostingSeekers, an award-winning go-to-directory for all things hosting. Our team conducts extensive research to filter the top solution providers, enabling visitors to effortlessly pick the one that perfectly suits their needs. We are one of the fastest growing web directories, with 500+ global companies currently listed on our platform.