Recursively Create a Nested Directory Structure & All Subfolders with a Single Command

May 27, 2014 - 10 Comments

Create 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 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.

.

Related articles:

Posted by: Paul Horowitz in Command Line, Tips & Tricks

10 Comments

» Comments RSS Feed

  1. shifu says:

    the most easy way: mkdir -p foo/bar/dir_name

  2. Vasco says:

    uh-uh, why is it giving me “Permission denied”?

    • Ramo says:

      Probably because you’re creating the folder within the root folder by using / at the start of the folder path such as:
      mkdir -p /FolderP/FolderSon/Subfolder.

      This will create FolderP (and its subfolders) in the root folder which starts with /.
      Maybe that’s why is asking for permission.

      If you WANT to create there (not recommended), use:
      sudo mkdir -p /FolderP/FolderSon/Subfolder
      and then type the password of your system.
      If you want to create it in the home folder (/Users//) use ~ before everything, such as:

      mkdir -p ~/FolderP/FolderSon/Subfolder,

      and if you want to create it at the same folder you are working on, then write:

      mkdir -p FolderP/FolderSon/Subfolder.

  3. jsan says:

    You can also include dots to create peer level directories. e.g.
    mkdir -p top/sub1/../sub2/../sub3

    to create:
    top
    top/sub1
    top/sub2
    top/sub3

  4. Eric says:

    `mkdir` is a real time saver. You can really build a great sequence/structure of folders.

    # Example, sort up an old disk:
    mkdir -p ~/test.d/fromUSB/{2010..2014}/{images/{png,jpg,gif},docs/{pdf,txt,rtf}}

    *(perhaps overkill, but for the example :))*

    • vdiv says:

      Nice script-fu!

      Now if you can recursively scan the USB drive and copy/move the corresponding file types in the right directory with a one-liner it will be great! :)

      • Eric says:

        Well, I think that would take more than a 1Liner. :)

        But that was just an example how you can “throw up” a folder structure before you clean up an old disk.

        – – –

        btw: If you add -v (verbose) you will get the output in terminal. (as verification)

        mkdir -pv ~/test.d/folder{1..5}

        I have made an alias for that in my .bash_profile

        alias mkdir=’mkdir -pv’

  5. MarkA says:

    Been doing this for years, it’s standard unix. Didn’t realise it was a problem! There’s a fairly useful command called ‘man’ as well. You can do things like ‘man mkdir’ and ‘man -k dir’, the first one tells you how to use a specific command, the second one searches for a keyword if you’re not sure of a command.

  6. Todd says:

    mkdir -p is best remembered as ‘parents’, it will create all parent directories as needed if they don’t already exist.

Leave a Reply

 

Shop on Amazon.com and help support OSXDaily!

Subscribe to OSXDaily

Subscribe to RSS Subscribe to Twitter Feed Follow on Facebook Subscribe to eMail Updates

Tips & Tricks

News

iPhone / iPad

Mac

Troubleshooting

Shop on Amazon to help support this site