linux, macos, windows,

Creating multiple directories at once

Jan 13, 2023 · 1 min read · Post a comment

“A good engineer is a lazy engineer” is a popular saying, meaning achieving a maximum output with minimum input. One of key things is automation of course. For those lazy engineers out there, here’s a command to create multiple directories at once (using a single command).

Subdirectories and folders are used interchangeably.

Prerequisites

  • Shell environment

Solution

Linux / macOS

Create multiple directories:

mkdir dir1 dir2 dir3

Create multiple subdirectories under a directory:

mkdir -p parent_dir/{subdir1,subdir2,subdir3}

You can take it from here and create a more complex subdirectory tree structure as well. For instance:

mkdir -p parent_dir/{subdir1/{subdir1_1,subdir1_2},subdir2,subdir3}

Or, even multiple folders with a suffix. Example:

mkdir index0{1..9}

Windows

On Windows however, you can do it with both CMD (Command-Prompt) and PowerShell, even though CMD’s mkdir calls PowerShell’s New-Item in the background.

Using Command-Prompt:

mkdir folder1 folder2 folder

PowerShell equivalent:

"folder1", "folder2", "folder3" | %{New-Item -Name "$_" -ItemType "Directory"}

Regarding subdirectories, in CMD you could run something like this for instance:

mkdir .\folder1\subfolder1, .\folder2\subfolder2, .\folder3\subfolder3

To do the same with PowerShell, it requires a bit more scripting, but you get the point.

Conclusion

Feel free to leave a comment below and if you find this tutorial useful, follow our official channel on Telegram.