How to mkdir only if a directory does not already exist?
Asked Answered
P

17

2901

I am writing a shell script to run under the KornShell (ksh) on AIX. I would like to use the mkdir command to create a directory. But the directory may already exist, in which case I do not want to do anything. So I want to either test to see that the directory does not exist, or suppress the "File exists" error that mkdir throws when it tries to create an existing directory.

How can I best do this?

Phobia answered 27/4, 2009 at 14:47 Comment(0)
G
4705

Try mkdir -p:

mkdir -p foo

Note that this will also create any intermediate directories that don't exist; for instance,

mkdir -p foo/bar/baz

will create directories foo, foo/bar, and foo/bar/baz if they don't exist.

Some implementation like GNU mkdir include mkdir --parents as a more readable alias, but this is not specified in POSIX/Single Unix Specification and not available on many common platforms like macOS, various BSDs, and various commercial Unixes, so it should be avoided.

If you want an error when parent directories don't exist, and want to create the directory if it doesn't exist, then you can test for the existence of the directory first:

[ -d foo ] || mkdir foo
Gowon answered 27/4, 2009 at 14:49 Comment(13)
the shortened example you use is exactly what you should not do. It is reversing the logic to save coding space but it should use ! and && and make more sense to those reading it.Stillbirth
On Windows8 it seems -p is no longer supported and it's the default behavior.Navigator
@AndreasLarsen This question is about mkdir on Unix-like systems, not on Windows. -p is required for POSIX/Single Unix Specification compliance, so anything that intents to comply with those specifications will support -p. Windows is entirely different, unless you use a POSIX emulation layer like Cygwin or MSYS.Gowon
I discovered something interesting today with mkdir -p, you can use brackets! {} to create "complex" directory tree in a command. See here: technosophos.com/2010/04/15/…Say
@MikeQ I 'd prefer || instead of && because then the whole line has the right exit status. Important if your shell runs with errexit or if that line is the last one in a function, switch-case, whatever.Mantelpiece
Or: mkdir somedir 2>/dev/null || trueBlockbusting
@Say That has nothing to do with mkdir; the shell expands such an expression to a discrete list of argument that are passed to mkdir.Shaveling
Does this work with a wild card before the intermediate steps? Ex /*/foo/bar/baz ?Woodchopper
@Woodchopper Yes, if the appropriate directories already exist at the level that the wild card is used. In the shell, glob expansion happens before passing the arguments to the program (mkdir, in this case). So if you had directories a, b, and c, mkdir -p */foo/bar/baz would be the same as mkdir -p a/foo/bar/baz b/foo/bar/baz c/foo/bar/baz. mkdir can take multiple arguments, and when given -p, it will create needed intermediate directories. Experiment with this yourself. Try echo mkdir -p */foo/bar/baz to see what command is run; or just try running it in a test directory.Gowon
@Say discovery is very handy. Please remember not to leave whitespaces in any part of this script.Stacistacia
@Say Awesome! Just realized it works for other commands as well, e.g. to create 3 files: touch {a,b,c}.txtConga
I prefer [ -d foo ] || mkdir foo. 🤗Spongin
For the case that foo may be a symlink to a folder: See: #794358Russophobe
H
258

This should work:

$ mkdir -p dir

or:

if [[ ! -e $dir ]]; then
    mkdir $dir
elif [[ ! -d $dir ]]; then
    echo "$dir already exists but is not a directory" 1>&2
fi

which will create the directory if it doesn't exist, but warn you if the name of the directory you're trying to create is already in use by something other than a directory.

Heavyset answered 27/4, 2009 at 14:50 Comment(3)
I don't think there's a -d operator in korn, rather -e is used for both files / directories and just checks existence. Also, they all return 0 upon success, so ! is redundant. Correct me if I'm wrong.Biopsy
wrong on both counts, AFAIK. tests return true on success, and -d exists too (at least on MacOS X)Heavyset
it might be worth mentioning that this isn't quite thread-safe. between the time that you check if the directory exists and the time you try to write, things might change.Anneliese
G
116

Use the -p flag.

man mkdir
mkdir -p foo
Gammer answered 27/4, 2009 at 14:50 Comment(1)
-p, --parents no error if existing, make parent directories as neededKaiserism
I
91

Defining complex directory trees with one command

mkdir -p project/{lib/ext,bin,src,doc/{html,info,pdf},demo/stat/a}
Involucel answered 21/8, 2013 at 20:8 Comment(6)
no error if existing, make parent directories as neededPoinsettia
Keep in mind that this is not a feature of mkdir itself, but the shell that executes the command. It's called brace expansion - AFAIK, only Bash, ksh, zsh, and the C shell support it.Whipcord
if you have spaces around the commas you may (will) get unexpected result. Beware.Gigi
@Gigi stated, you will need to escape characters that are normally part of regex. (i.e. instead of using folder name you need to use folder\ name)Lancers
This is not an answer; this is a separate topic that would be more appropriate as a comment.Geum
Defining? Do you mean creating?Hupp
R
55

If you don't want to show any error message:

[ -d newdir ] || mkdir newdir

If you want to show your own error message:

[ -d newdir ] && echo "Directory Exists" || mkdir newdir
Retract answered 27/3, 2015 at 3:57 Comment(2)
But subject to race conditions?Hupp
@PeterMortensen : Of course. Use mkdir -p if you do not want race conditions. But you won't get to show your own Directory exists error.Retract
L
49

mkdir foo works even if the directory exists. To make it work only if the directory named "foo" does not exist, try using the -p flag.

Example:

mkdir -p foo

This will create the directory named "foo" only if it does not exist. :)

Lupitalupo answered 20/1, 2014 at 12:23 Comment(1)
As mentioned by @BrianCampbell, this will also create any other directories in the path. This can be dangerous if e.g. a volume becomes unmounted, as it may create directories in the mount point.Effortless
A
38

You can either use an if statement to check if the directory exists or not. If it does not exits, then create the directory.

  1. dir=/home/dir_name

    if [ ! -d $dir ]
    then
         mkdir $dir
    else
         echo "Directory exists"
    fi
    
  2. You can directory use mkdir with -p option to create a directory. It will check if the directory is not available it will.

    mkdir -p $dir
    

    mkdir -p also allows to create the tree structure of the directory. If you want to create the parent and child directories using same command, can opt mkdir -p

    mkdir -p /home/parent_dir /home/parent_dir/child1 /home/parent_dir/child2
    
Acetal answered 21/8, 2018 at 4:56 Comment(2)
Is there any advantage in using if loop!? why would someone choose that over -p option?!Fumarole
I have shared you ways of creating the folders when an folder does not exists. It depends upon the requirement on the one. If you have a use case where you need to check if folder does not exist and you want to keep track of that so you can go with solution 1. If it does not matter, you can go with solution 2, it will create the folder if not exists.Acetal
D
30

Simple, silent and deadly:

mkdir -p /my/new/dir >/dev/null 2>&1
Drawl answered 15/12, 2020 at 16:57 Comment(6)
It should be: mkdir -p /my/new/dir >/dev/null 2>&1Sugary
mkdir -p my/new/dir does not complain if my/new/dir already exists, no need to redirect outputHopehopeful
Typo fixed. Leevi, I believe I ran into a situation where the output needed to be squelched.Drawl
note for naiive windows users: -p still fails on windows when the folder existsEvapotranspiration
Does mkdir -p /parent/dir complain if permissions prevent creating /parent/dir ? If so, perhaps the redirection might be considered "deadly" or at least unwise.Xerophilous
Sure, you're right, things can always go wrong for a number of reasons. If you need more requirements met than requested by the OP (permissions, invalid path, out of space...), you should script for them.Drawl
W
27

The old tried and true

mkdir /tmp/qq >/dev/null 2>&1

will do what you want with none of the race conditions many of the other solutions have.

Sometimes the simplest (and ugliest) solutions are the best.

Waterrepellent answered 27/4, 2009 at 14:47 Comment(5)
This will fail if "tmp" did not exist. nor does it give you any confirmation.Stillbirth
Race conditions? Can you elaborate in your answer? Non-atomic test & create?Hupp
@Peter, a snippet like (for example) [ -d newdir ] || mkdir newdir, where the directory does not initially exist, has a race condition in that, between the test for existence and the attempted creation, another process could swoop in and create the directory. Hence the mkdir would then fail.Waterrepellent
@Mike Q: the base path /tmp has likely been chosen in the example to represent a base-path that always exists and is write-able to the current user, e.g. the user has enough rights to create a directory in. You raise a valid point thought:: the logic is a bit contradictory, as when this command fails, it can mean two things: 1.) the directory exists or 2.) the directory could not be created. This is not true for the operation itself, therefore a simple post-check on the directory path can give the confirmation, or the next command that operates on.Juggler
mkdir -p /tmp/qq > /dev/null 2>&1 will create /tmp/ if missing.Mayence
F
18

mkdir does not support -p switch anymore on Windows 8+ systems.

You can use this:

IF NOT EXIST dir_name MKDIR dir_name
Flute answered 22/11, 2016 at 12:39 Comment(2)
OP was about AIX kornshell... nothing to do with Windows, does it?Escalate
Are you sure it was supported before? Do you have a source?Hupp
W
15
directory_name = "foo"

if [ -d $directory_name ]
then
    echo "Directory already exists"
else
    mkdir $directory_name
fi
Walkover answered 16/12, 2015 at 11:34 Comment(0)
Q
15

This is a simple function (Bash shell) which lets you create a directory if it doesn't exist.

#------------------------------------------#
# Create a directory if it does not exist. #
#------------------------------------------#
# Note the "-p" option in the mkdir        #
# command which creates directories        #
# recursively.                             #
#------------------------------------------#
createDirectory() {
   mkdir -p -- "$1"
}

You can call the above function as:

createDirectory "$(mktemp -d dir-example.XXXXX)/fooDir/BarDir"

The above creates fooDir and BarDir if they don't exist. Note the "-p" option in the mkdir command which creates directories recursively.

Quackery answered 14/2, 2018 at 18:54 Comment(0)
B
12

Or if you want to check for existence first:

if [[ ! -e /path/to/newdir ]]; then
            mkdir /path/to/newdir
fi

-e is the exist test for KornShell.

You can also try googling a KornShell manual.

Biopsy answered 27/4, 2009 at 14:51 Comment(0)
U
12

Referring to man page man mkdir for option -p

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

which will create all directories in a given path, if exists throws no error otherwise it creates all directories from left to right in the given path. Try the below command. the directories newdir and anotherdir doesn't exists before issuing this command

Correct Usage

mkdir -p /tmp/newdir/anotherdir

After executing the command you can see newdir and anotherdir created under /tmp. You can issue this command as many times you want, the command always have exit(0). Due to this reason most people use this command in shell scripts before using those actual paths.

Ur answered 21/1, 2019 at 23:46 Comment(0)
R
10

Improvement on the 'classic' solution (by Brian Campbell) - to handle the case of symlink to a directory.

[ -d foo/. ] || mkdir foo
Russophobe answered 3/8, 2022 at 14:21 Comment(0)
S
5
mkdir -p sam
  • mkdir = Make Directory
  • -p = --parents
  • (no error if existing, make parent directories as needed)
Snoop answered 20/4, 2018 at 9:30 Comment(0)
Y
3
if [ !-d $dirName ];then
     if ! mkdir $dirName; then  # Shorter version. Shell will complain if you put braces here though
     echo "Can't make dir: $dirName"
     fi
fi
Yeomanry answered 15/3, 2020 at 20:37 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.