Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Is there a linux command that I'm overlooking that makes it possible to do something along the lines of: (pseudo)

$ mkdir -R foo/bar/zoo/andsoforth

Or is there no alternative but to make the directories one at a time?

share|improve this question

migrated from serverfault.com Sep 27 '12 at 3:56

This question came from our site for system and network administrators.

2  
linux.die.net/man/1/mkdir – Zoredache Aug 7 '12 at 20:36
    
funny, I was looking at that page but totally overlooked "parent" because I was thinking of them as children (left to right). – Dr.Dredel Aug 7 '12 at 20:37
11  
Protip: In some shells you can even do mkdir -p foo/{bar,baz}/zoo/andsoforth. Very useful! – Aaron Copley Aug 7 '12 at 20:43
up vote 155 down vote accepted
$ mkdir -p foo/bar/zoo/andsoforth
share|improve this answer
20  
FYI the -p stands for "parents" – delinquentme Apr 22 '15 at 17:06
3  
They could've made -r instead, like any other command, that's impossible to remember specific parameters for every linux command – Vedmant Mar 21 at 2:56
    
But mkdir -p is not cross platform, doesn't work in every shell – Роман Коптев Apr 17 at 13:40
    
@РоманКоптев Feel free to post another answer then. – EEAA Apr 17 at 13:40
3  
@РоманКоптев, mkdir -p is specified in POSIX, which is the closest thing to being fully portable. What better do you expect in a shell script? – Wildcard Apr 18 at 18:54

Using of mkdir -p is a more simple way for most modern oses:

mkdir -p foo/bar/zoo/andsoforth

However, using of mkdir -p is not recommended in many manuals. Read documentation for the latest versions of gnu make and autoconf about problems with using mkdir -p:

The cross platform installation and configuration systems have theirs own safe alternatives for mkdir -p.

CMake to use in shell command line:

 cmake -E make_directory foo/bar/zoo/andsoforth

Autoconf to use in script with preprocessing:

AS_MKDIR_P(foo/bar/zoo/andsoforth)

or:

AC_PROG_MKDIR_P(foo/bar/zoo/andsoforth)

But these solutions require cmake or autoconf (M4) tools to be installed (and possible preprocessing)

You can use also install-sh script with -d option:

install-sh -d foo/bar/zoo/andsoforth

This script is used by autoconf and automake project. I think it must be the most safe solution.

At the time I was searching for cross platform solution for standard /bin/sh without dependences, but haven't found one. Therefore I wrote the next script that may be not ideal, but I think it is compliant to most cross platform requirements:

#! /bin/sh

cdirname() # cross platform alternative for 'dirname'
{
  # $1 - path
  test $# -eq 1 || { echo "Procedure 'cdirname' must have only one parameter. Scripting error."; exit 1; }
  echo "$1" | sed -n -e '1p' | sed  -e 's#//*#/#g' -e 's#\(.\)/$#\1#' -e 's#^[^/]*$#.#' -e 's#\(.\)/[^/]*$#\1#' -
}

mkd() # cross platform alternative for 'mkdir -p'
{
  # $1 - directory to create
  test $# -eq 1 || { echo "Function 'mkd' can create only one directory (with it's parent directories)."; exit 1; }
  test -d "$1"  && return 0
  test -d "$(cdirname "$1")" || { mkd "$(cdirname "$1")" || return 1; }
  test -d "$1" || { mkdir "$1" || return 1; }
  return 0
}

This script can be used for old systems, where option -p for mkdir is absent.

sed-based cross platform version of dirname was added to the code. It works with a way similar to dirname (correct with path /, paths with base name only, paths with trailing /, paths with and without trailing \ns). This function can't work correct if the path has newlines or some invalid characters for current locale. It also replaces any combination of / (//, ///) with single /

Changed line mkdir "$1" || return 1 to test -d "$1" || { mkdir "$1" || return 1; } because mkdir terminates with error if path exists and this check is needed for paths containing constructions like aaa\. (If aaa doesn't exist previous version creates aaa and then tries to create it again).

This version of mkd doesn't generate error if path already exist (but it still has the possibility to generate such an error in parallel execution) and can't get several directories in command line.

share|improve this answer
4  
"mkdir -p" is defined by posix. What unix or unix-like variants don't support it? (and it shouldn't be a shell built-in, though I guess there isn't any reason it couldn't be.) pubs.opengroup.org/onlinepubs/9699919799/utilities/mkdir.html – Tim B Apr 17 at 22:49
3  
Some of the information in that page is only 15 years old. Some is older. – Thomas Dickey Apr 17 at 23:30
3  
Systems that wouldn't support mkdir -p would be too old (and we're talking at least a couple of decades old) to have a printf utility or support -- to mark the end of options. – Stéphane Chazelas Apr 18 at 14:06
1  
printf -- "$1\n" is wrong. The first non-option argument to printf is the format. It should be printf '%s\n' "$1" – Stéphane Chazelas Apr 18 at 14:10
2  
Your cdirname would give the wrong answer for foo/bar//, or directory names containing newline characters, or invalid characters in the current locale. – Stéphane Chazelas Apr 18 at 14:12

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.