mkdir Command linux

Create Directory in Linux using mkdir Command

The popular Linux operating system has a complex and extensive world; the reliability and compatibility of Linux have made it a popular choice for users and is used to facilitate the performance of various tasks. As you know, the Linux terminal is one of its powerful features that makes it possible to perform a wide range of operations, from installing new programs to optimizing the operating system by any command. As a server administrator, your top priority should be to master Linux commands thoroughly.

Unlike Windows, Linux has a compatible and very flexible environment. Due to the flexibility of Linux in using server-side commands, professional and remote management of a Linux server is possible without a graphical environment. As a result, to do any work in the Linux environment, you need to be familiar with how to use the commands after buying a Linux VPS to make your work process with the Linux environment more effortless and practical.

In Linux VPS, knowing how to create a folder and directory is essential for any experienced or beginner administrator. Using the graphical interface, creating a directory in Linux is similar to Windows and Mac, etc., but sometimes we need to create a directory using the Command-line. For creating a folder in Linux, there are different commands and solutions that we want to do this process using themkdircommand.

This article will focus on Linux commands with a wide range of parameters for creating new folders and directories and will teach you how to create a directory in Linux using themkdircommand. So stay with us to thoroughly learn this process.

The mkdir command [Make Directory Command] in Linux

By using the command line and the file manager, Linux create a folder, so to create single or multiple folders and directories in Linux, use the command and script called mkdir. Linux’s mkdir command, which stands for make directory, is a helpful tool for this;

Using this command, you can define a new directory, and if there is already a directory with the same name in the same path you specified, it will return an error that such a directory already exists. The “mkdir” command also allows you to create multiple directories with a single command, set permissions, and perform many other operations.

Prerequisites for running the mkdir command

  • Linux or Unix operating system
  • Access to the command line terminal (to execute the command)
  • Having sufficient user permission to create new directory linux

Create Directory in Linux using mkdir Command

The syntax required to execute the mkdir command is as follows:

mkdir [option] dir_name

Any number of directory names can be passed as arguments to this command.

Choosing Alternatives to the mkdir Command for Making a Directory

Option / SyntaxDescription
mkdir dir_nameThis command will create a new directory inside the current one.
mkdir {dir1,dir2,dir3,dir4}You can use this command to create more subdirectories in the current directory. Be careful you don't use spaces between the symbols{}.
mkdir –p dir/path/new_dirThis command will enable you to establish a directory structure that includes the missing parent directories (if there are any)
mkdir –m777 dir_nameUsing this command, you can provide all users complete read, write, and execute access to a new directory.
mkdir –v dir_name(s)With the information provided by this command, you can successfully execute the mkdir command in the current directory.

Create Directory in Linux using mkdir Command in 5 step

To execute the mkdir command, if access is defined for admin, go to the terminal and ensure the necessary access and proper permissions to perform the mkdir command.

Note: To execute the commands correctly, pay attention to upper and lower case letters; In case of typing wrongly and without attention to the capitalization and smallness of the letters, the meaning of the commands may change completely.

1. Directory name transfer

To create a new directory in Linux, pass the directory’s name as an argument to the mkdir command. For example, if you want to Linux create new directory called Newdir, you need to run the following command:

mkdir newdir

2. Confirm directory

If you enter this command, a new directory will be created in your current directory. After successfully creating the directory, you should see a blank line in the terminal. The “ls” command can be used to inspect and verify a directory after it has been created.

ls –l

If you do not specify the full path along with the directory name, the directory is created in the current working directory. The directory from which you run the commands is known as the “current working directory.” If you want to change the current working directory, you can use thecdcommand.

3. Creating a directory in another location

When creating a new directory elsewhere, you must specify an absolute or relative file path to the parent directory. For example, if you want to create a new directory in the /tmp folder, enter the following command:

mkdir /tmp/newdir

4. Create a directory in the parent directory

If you try to create a directory in the parent directory where the user does not have the required permissions, you will get a permission denied error.

mkdir /root/newdir

output

mkdir: cannot create directory '/root/newdir': Permission denied

5. Using the -v option (–verbose) to check the directory

Sometimes after running the “mkdir” command, you get no indication that the directory was created or failed. The “-v” option of the “mkdir” command displays detailed information about the directory creation process. The -v (–verbose) option instructs Mkdir to send a message for each newly created directory.

Note: After getting the information from the directory creation, you can skip the “ls” command to check the generated directory because you already have all the information you need about its creation.

How to Create Parent Directories

The “-p:” option of the “mkdir” command allows you to directory creation in Linux with multiple and different subdirectories. By selecting this option, you can ensure that the missing parent directory is created automatically.

To create a subdirectory inside another directory in Linux, for example, here, if you want to add “dirtest2” to “dirtest1”, you must use the “mkdir” command with the full path, as in the following procedure:

mkdir –p Linux/dirtest1/dirtest2

After running the “mkdir” command with the full path, you can use the “ls” command with the “-R” option to verify that the directory was created.

ls-R

If you forget the “-p” option, you will get an error from the terminal saying that the specified directory does not exist.

IF:

mkdir Linux/dirtest3/dirtest2

Output

mkdir: cannot create directory ' Linux/dirtest3/dirtest2' : No such file or directory

As you can see, not only was the new parent directory not created, but you got an error about dirtest3 not existing.

You will also get the same error if the parent directories do not exist.

If the parent directories do not exist, you can save time by using themkdircommand with the -p option instead of creating them manually.

Note: The -p option can only be used to create a new directory if it does not already exist.

To create a directory that already exists and the –p option is not provided, you will get a File exists error:

mkdir newdir

output

mkdir: cannot create directory 'newdir': File exists

Create Directory in Linux using mkdir Command

Permission settings for directory creation

When creating any directory on a Linux system, the “rwx” permission is granted exclusively for the benefit of the user who created the directory. If you want to have specific permission for the created directory using the “mkdir” command, the “-m” or (-mode) option changes the directory permissions for all users.

For better understanding, let’s define an example where we set the permission as “777” for the new directory, which means that by setting this permission, the user who created the directory will have access to the file, and the user can also read, write and execute the file.

mkdir -m {permissions} {dirName}

 

For example:  

mkdir -m 777 newdir

Next:

ls –l

If you decide to check the new directory’s specifications and permissions, you should be able to access an extensive list of directories by typing “ls -l” at the command line.

Note: If you do not specify the -m option when creating a new directory, newly created directories corresponding to the unmask value will be specified with permissions of 775 or 755 by default.

Creating multiple directories

The “mkdir” command can be used to separately create multiple directories within a given directory, While you have to spend more time executing the commands individually. In other words, the mkdir command can also be used to launch a complex directory structure with a single command. Therefore, if you are looking for a solution that does not waste a lot of time running separate commands and can quickly create multiple directories, using the “mkdir” command is the best option. In the “mkdir” command, you can separate the names of different directories by commas and add a list of directories to a directory.

For example, look at this for creating multiple folders at once.

mkdir {test1,test2,test3}

Note: You don’t need to leave space inside {} between directory names; By putting a distance, You will need a new name.

Consider the following example:

mkdir -p Art/{ Cubism / Fauvism, Impressionism , Realism , Symbolism /{ Expressionism, Constructivism , Abstract }, Romanesque /Baroque/ Rococo }

The tree directory you see below is the result of the above command:

Art/
|-- Romanesque
| `-- Baroque
| `-- Rococo
|-- Realism
|-- Impressionism
|-- Cubism
| `-- Fauvism
`-- Symbolism
|-- Expressionism
|-- Abstract
`-- Constructivism

FAQ

If you receive a "permission denied" message, it means that you lack the necessary authorization to create the specified directory at the given path. Check out if changing the group membership or ownership will solve the issue and get you access to the full directory path.

To be able to set permissions, the -m(-mod) option must be used.

The -p option can be used to create a parent directory.

The lscommand displays a list of files or directories in Linux and other Unix-based operating systems. The lscommand will default list all files and folders in the current directory, much like the File explorer or Finder in a graphical user interface.

Conclusion

In Linux, it is possible to create a directory in various ways and commands, which is an essential issue for Linux server administrators. One of the effective ways to create a directory in Linux is implemented by themkdircommand. If you don’t have an internal directory, you can use other options, and if you want to create a directory recursively, you can use the “-P” option.

This article taught you everything you need to know about themkdircommand to create directories and folders in Linux. You can take advantage of the mkdir command and its various options in directory creation. Just pay attention to the uppercase and lowercase letters when entering the commands in the command line because any mistake in the commands will lead to receiving an error message.

We hope this tutorial has fully guided you through the process of creating a directory in Linux with themkdircommand. Put your questions or concerns about this topic in the comments.

Leave a Reply

Your email address will not be published. Required fields are marked.


Helen Publish in May 24, 2023 at 6:23 am

is mkdir the only way for linux folder create?

    Liosa F Publish in May 26, 2023 at 11:21 pm

    yes

Thomas Publish in May 24, 2023 at 6:09 am

What is the easiest way to Linux how to create a directory?

    Liosa F Publish in May 26, 2023 at 11:20 pm

    The only and easiest way is mkdir command.

William Publish in May 24, 2023 at 5:58 am

How do we set permissions when Linux make directory?

    Liosa F Publish in May 26, 2023 at 11:19 pm

    Invoking the mkdir command with the -m (-mode) option allows you to create a directory with particular permissions.