How to Create a Linux Directory Using mkdir Command

Try this guide with our instant dedicated server for as low as 40 Euros

mkdir Command

In Linux, directories are the basic component of the file management system. Directories provide a logical structure and help with multi-level access control. Sysadmins and users depend on directories for system backup and process automation. 

Overall, they make it easy to find and manage files and user content. Without them, the files would be all over the place, making the system messy and very difficult to maintain.

So, Linux offers mkdir (short for make directory), a handy command for creating directories in Linux. This tutorial introduces mkdir and then discusses several use cases where you can use this command to manage files (and directories) on your Linux system. 

Let’s learn how to create a Linux directory using the mkdir command.

Table Of Content

  1. The Basic Syntax of the mkdir Command
  2. The mkdir Options
    1. The mkdir Command Options/Syntax List
  3. The mkdir Command Usage
    1. Create a New Directory
    2. Create Multiple Directories
    3. Make Parent Directories
    4. Verify Directories
    5. Resolve mkdir ‘Permission Denied’ Error
    6. Remove a Directory
  4. Conclusion
  5. FAQs

The Basic Syntax of the mkdir Command

The simplest use of the mkdir command is to create new directories in Linux systems. It’s included in the core Linux commands and thus available on all mainstream Linux distributions. 

The following basic syntax creates a directory:

# mkdir [option] dir_name

The mkdir Options

The mkdir command is very flexible and comes with various options that modify the impact of the command. You can list these options with the – -help.

# mkdir --help

mkdir help command output

The mkdir Command Options/Syntax List

mkdir command options and syntax summary

The mkdir Command Usage

Let’s explore some use cases of the mkdir command in Linux. Note that before trying out the following examples, you should have a basic understanding of Linux commands and terminal operations.

Create a New Directory

The primary use of the mkdir is to create a single directory with the following syntax:

# mkdir new_directory

mkdir new directory

Replace the new_directory with your desired directory name. This creates a new directory in the current location. 

If you want to check which directly you’re currently in, use the pwd (print working directory) command:

pwd command output

You can verify the newly created directory using the ls (list) command:

mkdir new directory ls

The Verbose Mode for the mkdir Command

Using the verbose mode (-v) with the mkdir command in Linux is helpful when you want more detailed information about the actions performed by the command.

Here’s how to use the -v option to get the required output:

# mkdir -v [directories]

mkdir -v new directories

Create Multiple Directories

You can use the basic mkdir command to create multiple directories in a single command:

# mkdir dir1 dir2 dir3

mkdir dir1 dir2 dir3

This command simultaneously creates three different directories: dir1, dir2, and dir3. These directories exit at the same level.

mkdir dir1 dir2 dir3 ls

Use the ls command to see the newly created directories.

Make Parent Directories

To create parent directories along with the target directory in Linux, use the mkdir command with the -p option. This option ensures the entire directory path, including any necessary parent directories, is created. The syntax is as follows:

# mkdir -p parent_dir/child_dir

mkdir -p parent_dir child_dir

This command creates both the parent_directory and target_directory, even if parent_directory doesn’t exist. The -p option simplifies creating nested directories in one go.

Set Permissions When Making a Directory

When setting permissions for a directory in Linux, you are specifying who can read, write, and execute files within that directory. 

Permissions are typically set for three user categories: owner, group, and others

The owner has specific rights, the group has separate permissions, and others (everyone else) have their access levels. These settings ensure precise control over directory and file access.

Use the -m flag with the mkdir command to set permissions during directory creation:

# mkdir -m 755 secure_dir

mkdir -m 755 secure dir

This command sets the permissions of secure_dir to 755. 

Here, the owner has complete control; the group can read and execute, while others can also read and execute.

Verify Directories

After creating a directory, we recommend using the ls command to verify that the directory exists at the location:

# ls -l

ls -l

The -l option provides a detailed listing, including information about permissions, ownership, directory size, and modification time.

Resolve mkdir ‘Permission Denied’ Error

Encountering the permission denied error while creating a directory creation means you lack the required permissions for your current location. To resolve this, use the sudo command to gain root privileges and create the directory.

opt$ mkdir

For instance, to create a directory named redswitches with root privileges. While executing this command, you might be asked to enter the root password.

# sudo mkdir redswitches

opt$ mkdir directory

Remove a Directory 

Use the rmdir (short for remove directory) command to delete an empty directory.

rmdir new directory

Once you hit Enter, the command will remove the directory. 

The rm command is a more extreme version which used to remove a directory and its contents forcefully and recursively. 

# rm -rf directory_name

rm rf new directory

-r: This flag is used for recursive actions, and the command will remove the directory and its contents, including all subdirectories and files.

-f: This flag is used to suppress error messages and prompts, making the deletion process non-interactive and more aggressive.

Using the ‘rm -rf’ command is highly risky, especially when applied to critical directories like the root home directory. Beware of the consequences when using this command, as discussed in this guide.

Conclusion

Mastering the mkdir command in Linux is crucial for efficient file system management in Linux. We discussed different use cases, from creating a file to removing it, in this comprehensive guide. Be sure to follow these steps and create a Linux directory without worry.

For those seeking reliable server hosting solutions, consider RedSwitches Bare Metal Hosting Provider, which offers robust and secure hosting services ideal for Linux-based applications. Whether you need a dedicated or traffic-friendly 10Gbps dedicated server, we are your trusted hosting partner.

FAQs

Q. Can I undo a mkdir command?

No, you cannot “undo” a mkdir command directly. You’d have to remove the directory using the rmdir or rm command.

Q. How can I make a directory with spaces in its name?

If the directory’s name contains space, use quotes around the name: mkdir “new directory.”

Q. Is mkdir case-sensitive?

Yes, like most Linux commands, mkdir is case-sensitive.

Q. Can mkdir create a directory in a different location?

Yes, provide the full path while creating a directory: # mkdir /path/to/new_directory. Otherwise, it can create a directory in a different location.

Q. Can I create a directory with special characters in its name using mkdir?

Yes, but you should use quotes or escape characters. For example, mkdir “# my#directory” or # mkdir my\#directory.

Q. What occurs if I attempt to make a directory that already exists?

By default, mkdir will give an error if the directory already exists. To avoid this, you can use the -p option, which will not create the directory again but won’t show an error.

Q. Can I create a directory with the same name as a file?

No, you cannot have a directory and a file with the same name in the same location. Linux differentiates files and directories but does not allow them to share names in the same directory.

Q. How can I create directories with permissions for a specific group or user?

You can combine mkdir with chown and chmod commands to set specific user or group ownership and permissions. For example: # mkdir new_dir && chown user:group new_dir && chmod 70 new_dir. ‘&&’ here is used for concatenation (combining the commands). 

Q. Is creating a directory in another user’s home directory possible?

Yes, if you have the necessary permissions or use a superuser account. Specify the full path to that user’s home directory in your mkdir command.

Q. How do I check the version of mkdir I am using?

You can check it by typing # mkdir –version in the terminal, and it’ll display the version.

Q.  Instead of using the command line, are there any GUI alternatives to the mkdir command for creating directories in Linux?

Yes, most file managers in Linux GUIs allow you to create directories through right-click context menus or other graphical interfaces.

Try this guide with our instant dedicated server for as low as 40 Euros