how to mkdir multiple directories?
If you want to create a directory containing several subdirectories, or a directory tree, using the command line in Linux, generally you have to use the mkdir command several times. However, there is a faster way to do this.
Let’s say we’ve created a directory called htg, and want to create four subdirectories within it. In a normal situation, we’d use the mkdir command to create the htg directory. Then, we’d need the cd command to change to the new htg directory and, finally, we we’d use the mkdir command again four times to create the four subdirectories.
This can all be combined into one command, and we’ll show you how.
To create a new directory with multiple subdirectories you only need to type the following command at the prompt and press Enter (obviously, change the directory names to what you want).
The -p flag tells the mkdir command to create the main directory first if it doesn’t already exist (htg, in our case). The words in the brackets are part of the “brace expansion list”. Each of the items in the brace expansion list is appended separately to the preceding path (htg/).
For example, the above command is expanded into htg/articles, htg/images, htg/note, htg/done, all four of the subdirectories being created under the htg directory. As you can see in the screenshot below, it worked.
You can also use a brace expansion list in the mkdir command if you’re creating subdirectories in a directory that already exists, as shown below. In this example, the htg directory already exists so the subdirectories are simply added under that directory.
You can also nest brace expansion lists in the mkdir command. For example, in the articles subdirectory under the htg directory, we want to create two subdirectories called new and rewritten. So, we type the following command at the prompt and press Enter.
You can also use the full path if you want, as I’ve done in the example below:
The four subdirectories are created under the htg directory and then the two subdirectories, new and rewrites, are created under the articles subdirectory.
RELATED: How to Make a New Directory and Change to It with a Single Command in Linux
Here we will show couple of ways/tips/hints on how to create multiple directories in Linux. The steps we want to cover in this article are how to create multiple directories in linux and sub-directories with the mkdir tool and for each individual example to create the directories with only one command. In other words, we want to run one mkdir command in terminal that will enables us to create multiple directories and sub-directories at once. For those are curios and keen on to learn, we also covered the steps on how to delte folders in this >>>POST<<<.
Just to recap first how we use mkdir to create a directory:
This single command will of course create just one singe directory where are your currently positioned in your Linux system. As we mentioned where are now going to show examples how to create multiple directories and sub-directories. In these examples we’re also going to use the mkdir tool. It’s still powerful to do all the directory creation examples we’re going to show.
This is an example where we need to create a single parent directory(or upper level directory) where inside of it, we need to create multiple sub-directories, all at once with one command. To do this, run the mkdir tool in a command like this:
When the mkdir tool is ran like this, this will allow us the create multiple directories at once for this case.This is because of the -p argument, which designates the first in line directory(our main directory) as a parent directory which then enables us to add arguments for creating sub-directories inside of the main directory.
Small note, there must not be any spaces between the sub-directory names, otherwise the command will not work.
If you just need to create multiple directories in the in the currently positioned directory without having a main directory or creating a directory tree, we can just use brackets from example 1 and do just that:
What does this mean is, if we have, or need to create a directory path like the following:
directory1/directory2/directory3
we can also achieve this with the mkdir tool, Run the mkdir as following:
What we mean by this is – we can nest brackets from the example 1 and create multiple sub-directories inside sub-directories and all of them within the main directory.
With a command like this, you can instantly create a directory tree.
Mkdir tool also has options to set the permissions as you want(as you have assigned) and create a new directory at the same time.
You can also combine this option with other examples we showed earlier and use to set a folder permission at once on multiple directories.
For an example, we’re now going to create a parent directory with multple sub-directories and have them all the same permissions:
We showed 5 different examples or use cases on how to create multiple directories in linux with the mkdir tool by running a single command in terminal. Mkdir is a powerful tool for creating directories and it has many more options for directory creation(which can be seen in the mkdir man page).
The beauty of this tool that it’s options can be combined and the examples above we showed can also be combined to create directory trees with even more complexity or for many more use cases. It is highly suggested to go through the mkdir man page(usage documentation or manual). Link to the man page – mkdirmanpage.
How to Create Multiple Directories with mkdir. You can create directories one by one with mkdir, but this can be time-consuming. To avoid that, you can run a single mkdir command to create multiple directories at once. To do so, use the curly brackets {} with mkdir and state the directory names, separated by a comma.
- Click Folder > New Folder. Tip: You can also right-click any folder in the Folder Pane and click New Folder.
- Type your folder name in the Name text box.
- In the Select where to place the folder box, click the folder under which you want to place your new subfolder.
- Click OK.
As Linux users, we use files and directories on a regular basis. Files allow us to store important data whereas directories allow us to organize files in a proper way. In addition to this, we often create a hierarchical directory structure to organize the contents in a better way.
In this beginner-friendly article, we will learn about the mkdir command. As the name suggests, the mkdir command is used to create a named directory at a given path, which also allows us to create single or multiple directories at once with the required file permissions.
We should note that to use the mkdir command the user must have the required permissions on the parent directory, or else the command will fail with the permission denied error.
Just like other Linux commands, the syntax of the mkdir command is mainly divided into two groups – options and arguments:
In the above syntax, the square brackets ([]) represent the optional arguments whereas angular brackets (<>) represent the mandatory arguments.
As the name implies, the mkdir is a short form of the “make directory”. The good thing is that it creates a directory only if a directory or file with the same doesn’t exist at the given path. In this way, this is a very safe command and doesn’t cause any harm to the system.
In this section, we will see the basic usage of the mkdir command with examples.
One of the fundamental use of the mkdir command is to create a named directory at a given path. So let’s create a directory with the name rpm-distros in the current working directory:
Now, use the ls command to verify that the directory has been created:
In the first example, we used the relative path with the mkdir command. However, this command also supports the absolute path.
We can use the pwd command or the pwd environment variable to find the absolute path of the current working directory.
So, let’s create the named directory – deb-distros in the current working directory using the absolute path:
Now, verify that the new directory has been created in the current working directory:
The mkdir command accepts multiple paths as an argument, which allows us to create multiple directories in one go.
Let’s create three directories inside the deb-distros directory using the single command:
Now, let’s list the contents of the deb-distros directory:
As we can see, the mkdir command created multiple directories successfully.
In the previous example, we saw how to create multiple directories inside another directory using a single command. However, that wasn’t the most efficient way because we specified the parent directory name i.e. deb-distros with each sub-directory.
To overcome this limitation, we can specify the sub-directory names in a brace expansion as shown in the following example, where we create three sub-directories inside the rpm-distros directory:
Here, we should note the following two important points:
Now, let’s verify that the required directory structure has been created successfully:
In the previous sections, we saw how to create multiple directories. However, that approach doesn’t work if we wish to create a nested directory structure. In such a case, we can use the -p option of the command that creates the parent directory if required.
Let’s, create a nested sub-directory structure:
Now, verify the contents of the rpm-distros/centos directory in a recursive way:
As we can see, the command created the required directory structure without reporting the error for the existing parent directories. This option comes in very handy while writing shell scripts. We can use it to suppress the directory creation error which might occur due to the existing directory.
Sometimes, we need to modify the access permission of the directory immediately after its creation. In that case, we have to use the two commands – mkdir and chmod. However, we can achieve the same result using a single command.
Let’s use the -m option to set access permissions on a directory while creating it:
In this example, we used the numeric format to set the access permission. In a similar way, we can use the textual format.
For example, we can achieve the same result using the below command:
Now, use the ls command to find out the access permission of the directories:
By default, the mkdir command doesn’t print anything on the terminal after the directory creation. Hence, so far we have been using the ls command to verify whether or not the directory has been created.
To overcome this limitation, we can use the verbose mode of the command that prints the message for each created directory. This option gives meaningful information when we combine it with the –p option:
Let’s use the -v option with the command to enable the verbose mode:
As Linux users, we use files and directories on a regular basis. Files allow us to store important data whereas directories allow us to organize files in a proper way. In addition to this, we often create a hierarchical directory structure to organize the contents in a better way.
In this beginner-friendly article, we will learn about the mkdir command. As the name suggests, the mkdir command is used to create a named directory at a given path, which also allows us to create single or multiple directories at once with the required file permissions.
We should note that to use the mkdir command the user must have the required permissions on the parent directory, or else the command will fail with the permission denied error.
Just like other Linux commands, the syntax of the mkdir command is mainly divided into two groups – options and arguments:
In the above syntax, the square brackets ([]) represent the optional arguments whereas angular brackets (<>) represent the mandatory arguments.
As the name implies, the mkdir is a short form of the “make directory”. The good thing is that it creates a directory only if a directory or file with the same doesn’t exist at the given path. In this way, this is a very safe command and doesn’t cause any harm to the system.
In this section, we will see the basic usage of the mkdir command with examples.
One of the fundamental use of the mkdir command is to create a named directory at a given path. So let’s create a directory with the name rpm-distros in the current working directory:
Now, use the ls command to verify that the directory has been created:
In the first example, we used the relative path with the mkdir command. However, this command also supports the absolute path.
We can use the pwd command or the pwd environment variable to find the absolute path of the current working directory.
So, let’s create the named directory – deb-distros in the current working directory using the absolute path:
Now, verify that the new directory has been created in the current working directory:
The mkdir command accepts multiple paths as an argument, which allows us to create multiple directories in one go.
Let’s create three directories inside the deb-distros directory using the single command:
Now, let’s list the contents of the deb-distros directory:
As we can see, the mkdir command created multiple directories successfully.
In the previous example, we saw how to create multiple directories inside another directory using a single command. However, that wasn’t the most efficient way because we specified the parent directory name i.e. deb-distros with each sub-directory.
To overcome this limitation, we can specify the sub-directory names in a brace expansion as shown in the following example, where we create three sub-directories inside the rpm-distros directory:
Here, we should note the following two important points:
Now, let’s verify that the required directory structure has been created successfully:
In the previous sections, we saw how to create multiple directories. However, that approach doesn’t work if we wish to create a nested directory structure. In such a case, we can use the -p option of the command that creates the parent directory if required.
Let’s, create a nested sub-directory structure:
Now, verify the contents of the rpm-distros/centos directory in a recursive way:
As we can see, the command created the required directory structure without reporting the error for the existing parent directories. This option comes in very handy while writing shell scripts. We can use it to suppress the directory creation error which might occur due to the existing directory.
Sometimes, we need to modify the access permission of the directory immediately after its creation. In that case, we have to use the two commands – mkdir and chmod. However, we can achieve the same result using a single command.
Let’s use the -m option to set access permissions on a directory while creating it:
In this example, we used the numeric format to set the access permission. In a similar way, we can use the textual format.
For example, we can achieve the same result using the below command:
Now, use the ls command to find out the access permission of the directories:
By default, the mkdir command doesn’t print anything on the terminal after the directory creation. Hence, so far we have been using the ls command to verify whether or not the directory has been created.
To overcome this limitation, we can use the verbose mode of the command that prints the message for each created directory. This option gives meaningful information when we combine it with the –p option:
Let’s use the -v option with the command to enable the verbose mode:
Now, let’s observe the output of the command:
More Questions
- What about beauty salons opening?
- How to create ec2 windows instance in aws step by step?
- Why alaska leaving pba?
- How to become a jeff bezos?
- What is crashing in project management?
- How to calculate qh and qc?
- Connect to aws lightsail ssh?
- What is the lmr list?
- What is picosure laser treatment?
- What does it take to get diabetes?