mkdir multiple directories at once

5
(1)

The ability to create multiple directories using mkdir at once is always useful when you need numbered directories, for example when working with archives or for testing purposes. However, creating the directories manually would be very boring and time-consuming if there were a large number of directories.

mkdir multiple directories at once

for i in {1..10}
        do mkdir “$i”
done

But thanks to the versatile options in the command line, this task can be solved quickly and easily using for loops.

mkdir multiple directories at once in Linux

Here I’ll show you usefull examples how to create multiple directories at once using mkdir here in the Linux bash with just one command.

$ for i in {1..10}; do mkdir dir"$i" ;done

quote The number of sequenses between the curly braces can be increased as desired, for example start at 1 and go to 100.

Listing the current directory shows the following output after mkdir.

user@linux:~$ ls -al
total 314452
drwxr-xr-x 18 user user      4096 Jan  7 16:46 .
drwxr-xr-x  4 user user      4096 Oct 11 15:56 ..
drwxr-xr-x  2 user user      4096 Jan  7 16:46 dir1
drwxr-xr-x  2 user user      4096 Jan  7 16:46 dir10
drwxr-xr-x  2 user user      4096 Jan  7 16:46 dir2
drwxr-xr-x  2 user user      4096 Jan  7 16:46 dir3
drwxr-xr-x  2 user user      4096 Jan  7 16:46 dir4
drwxr-xr-x  2 user user      4096 Jan  7 16:46 dir5
drwxr-xr-x  2 user user      4096 Jan  7 16:46 dir6
drwxr-xr-x  2 user user      4096 Jan  7 16:46 dir7
drwxr-xr-x  2 user user      4096 Jan  7 16:46 dir8
drwxr-xr-x  2 user user      4096 Jan  7 16:46 dir9

In Linux bash and zsh it’s even easier, as the mkdir sequenses shown.

$ mkdir dir{1..10}

If there should now be a file in each directory, you can do this with the next command.

$ for i in {1..10}; do echo -e "This is the message" > dir"$i"/readme.txt ;done

This command create a file in multiple directories at once using for loop .

If you want to set the owners and permission to multiple directories at once then this command is used.

$ for i in {1..10}; do chown -R user:user dir"$i" ;done
$ for i in {1..10}; do chmod -R 755 dir"$i" ;done

If you do not need the directories anymore and can to be deleted again, this command can do it just as quickly at once in the Linux shell.

$ for i in {1..10}; do rm -rf dir"$i" ;done

Create multiple directories in Windows at once

In Windows you can open notepad and insert the following line and save it as a batch file (.bat). Then open a command prompt and simply run the batch file.

@echo off
for /L %%n in (1,1,10) do mkdir c:\temp\dir%%n

Alternatively, this line also works and create multiple directories at once using mkdir in command prompt.

for /L %%n in (1,1,10) do md dir%%n

This command create multiple directories at once using in batch file.

Create multiple directories using PowerShell

In Windows PowerShell this can be done with this command.

for ($i=1;$i -le 10;$i++){MD "dir$i"}

This PowerShell commnad create multiple directories at once in a loop.

With a less neasty PowerShell command can do the same.

1..10 | ForEach-Object { New-Item -ItemType Directory -Path "C:\Temp\dir$_"}

This PowerShell command create multiple directories at once using the ForEach-Object.

How useful was this post?

Click on a star to rate it!

Average rating 5 / 5. Vote count: 1

No votes so far! Be the first to rate this post.

We are sorry that this post was not useful for you!

Let us improve this post!

Tell us how we can improve this post?

Leave a Reply

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