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

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

蓝颜夕 发布于 2021-11-28 字数 444 浏览 853 回复 16 原文

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?

如果你对这篇文章有疑问,欢迎到本站 社区 发帖提问或使用手Q扫描下方二维码加群参与讨论,获取更多帮助。



需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。


酒绊 2022-06-07 16 楼
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"
思慕 2022-06-07 15 楼
mkdir -p sam
  • mkdir = Make Directory
  • -p = --parents
  • (no error if existing, make parent directories as needed)
錯遇了你 2022-06-07 14 楼

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.

波浪屿的海角声 2022-06-07 13 楼

Or if you want to check for existence first:

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

-e is the exist test for KornShell.

You can also try googling a KornShell manual.

燃情 2022-06-07 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.

彼岸花ソ最美的依靠 2022-06-07 11 楼

Simple, silent and deadly:

mkdir -p /my/new/dir >/dev/null 2>&1
素手挽清风 2022-06-07 10 楼
directory_name = "foo"

if [ -d $directory_name ]
    echo "Directory already exists"
    mkdir $directory_name
三月梨花 2022-06-07 9 楼

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

You can use this:

IF NOT EXIST dir_name MKDIR dir_name
德意的啸 2022-06-07 8 楼

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 ]
         mkdir $dir
         echo "Directory exists"
  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
请远离我 2022-06-07 7 楼

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.

祁梦 2022-06-07 6 楼

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.


mkdir -p foo

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

も让我眼熟你 2022-06-07 5 楼

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
埋葬我深情 2022-06-07 4 楼

Defining complex directory trees with one command

mkdir -p project/{lib/ext,bin,src,doc/{html,info,pdf},demo/stat/a}
想挽留 2022-06-07 3 楼

Use the -p flag.

man mkdir
mkdir -p foo
旧伤慢歌 2022-06-07 2 楼

This should work:

$ mkdir -p dir


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

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.

裸钻 2022-06-07 1 楼

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