I want to check recursively if directories in a given path exist; if they do not, I want to create them. So if I have the path /dir1/dir2/dir3/dir4, if want to check first if dir1/ exists, and if not, create it, then do the same for dir2/ etc.

  • For situations where mkdir -p doesn't cut it because you aren't actually looking to create the directory tree, consider looking at basename and dirname.
    – user
    Commented Sep 20, 2013 at 12:30
  • I thought of using basename and dirname, but the problem was that it separates only the last directory from the rest of the tree.
    – MBR
    Commented Sep 20, 2013 at 12:34
  • 1
    Put them in an array and iterate over the array in reverse order if you need to create a directory hierarchy (or do something else magical to a path in a top-to-bottom directory approach) without resorting to mkdir -p. Bash shell scripts are actually pretty powerful and can definitely do much more than just invoking simple commands.
    – user
    Commented Sep 20, 2013 at 12:39
  • What would be the most efficient way to put them in an array?
    – MBR
    Commented Sep 20, 2013 at 13:45
  • 1
    RE: putting them into an array, see this answer (obviously replace ; with /).
    – evilsoup
    Commented Sep 20, 2013 at 23:31

2 Answers 2


You can just tell mkdir to make the paths with the -p switch:

$ mkdir -p /dir1/dir2/dir3/dir4

excerpt from mkdir man page

   -p, --parents
          no error if existing, make parent directories as needed


  • Thanks, it is definitely much better than reinventing the wheel as I did with a while loop.
    – MBR
    Commented Sep 20, 2013 at 12:08
  • @MBR - yeah the existence of this command switch isn't that intuitive until you've seen it before, then it makes total sense.
    – slm
    Commented Sep 20, 2013 at 12:09

I found a solution, using cut and a while-loop



while [ $(echo $RUNDIR | cut -d '/' -f$(printf $n)) ]; do
      TMPDIR=$TMPDIR'/'$(echo $RUNDIR | cut -d '/' -f$(printf $n))
      if [ -a $TMPDIR ]; then
         echo $TMPDIR' exists'
         echo $TMPDIR' does not exist; creating directory...'
         mkdir $TMPDIR
      n=$(printf $(($n + 1)))

Is there a more compact way to do it?

  • Was this an immediate self-answer?
    – user
    Commented Sep 20, 2013 at 12:29
  • 1
    Lines 9 through 14 (also counting newlines, lines 7-12 of content) can be replaced with something like test -a "$TMPDIR" || mkdir "$TMPDIR" if you don't need the output as well. The final n increment can be written n=$(($n + 1)), no need for printf there that I can see. By using basename and dirname you don't need to parse the path yourself. (And always quote your variables unless you are positive that they will never contain spaces, and even so, you should quote them anyway just in case.) And even with that, mkdir -p as pointed out by @slm is probably a better answer. :)
    – user
    Commented Sep 20, 2013 at 12:31
  • It was; I was testing things while writing the question, and I figured out this was working though not very compact. Thanks for the improvement suggested!
    – MBR
    Commented Sep 20, 2013 at 12:32

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .