Create a nested directory structure with mkdir commandCreate a nested directory structure with mkdir command Creating a series of nested directories within one another can be done instantly through the command line. This makes it very easy to immediately and recursively create a complex directory structure of folders within subfolders of subfolders, without having to navigate manually into each directory to create a new directory, then navigate again to that subdirectory to create yet another directory, and so on. Instead, a command line trick will create the complete intermediate directory path in one fell swoop.

Building a nested directory structure the easy way requires the usage of the familiar mkdir command, which is routinely used to create a single new folder, but with the attachment of the -p flag to specify a full path to create. If you want to try this yourself, launch the Terminal app as found in the /Applications/Utilities/ folder and follow along to see how to use mkdir -p to build a series of directories within a single command line using a specified path.

Creating a Directory Structure Recursively by Specifying a Path

At it’s most simple form, you just specify the path like so to mkdir:

mkdir -p /path/to/make/

The -p flag insures that all subfolders are made recursively and in the appropriate place.

As an example, let’s say the nested directory path we want to create is “/Create/These/Folders/Within/Each/Other/” and none of these folders or subfolders currently exist. To instantly make them all, just use the following command string:

mkdir -p ~/Create/These/Folders/Within/Each/Other/

This will make the “Create” folder as the parent directory followed by the full series of “/These/Folders/Within/Each/Other/” as the appropriately nested child directories.

You can specify as long of a path as you want to build and it will instantly create the parent and all intermediate child directories.

Verifying the Directory and All Subfolders Were Created

To quickly double-check that all directories were built and that everything worked as intended by using the ‘find’ command like so:

find (parent directory) -type d -print

Using the above example again, the find command would be like so:

find ~/Create/ -type d -print

The output of this command would look something like the following, recursively listing out from the parent directory to all child folders:

$ find ~/Create -type d -print
/Create
/Create/These
/Create/These/Folders
/Create/These/Folders/Within
/Create/These/Folders/Within/Each
/Create/These/Folders/Within/Each/Other

Of course, you can also turn to the Finder to verify that a complex folder structure has been built, perhaps most easily viewed from the “List” view and then using the triangles to recursively open each subdirectory and show it’s contents, looking something like the following:

Finder view of nested folder path created from the command lineFinder view of nested folder path created from the command line

(Note the .DS_Store files are shown due to all hidden files being visible)

This is a really useful tip that we covered a while back as part of a handful of useful command line tricks, but considering the convenience it’s well worth covering on its own.

And yes, using the Terminal is by far the quickest way to accomplish this, as there is no similar trick specific to the Mac Finder, though one could theoretically automate nested directory creation through the Automator app in OS X if so desired. For what it’s worth, the mkdir command works the same in both Mac OS X and linux, so you can use it across platforms if desired. Want some more command line tricks? We’ve got you covered.

Source

Follow Me:
Top Best Sellers!!