Skip to content
Fix Code Error

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

March 13, 2021 by Code Error
Posted By: Anonymous

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?

Solution

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
Answered By: Anonymous

Related Articles

  • error LNK2005: ✘✘✘ already defined in…
  • What is the list of valid @SuppressWarnings warning…
  • What is the origin of foo and bar?
  • How to get $HOME directory of different user in bash script?
  • What is your most productive shortcut with Vim?
  • Javax.net.ssl.SSLHandshakeException:…
  • How to permanently set $PATH on Linux/Unix?
  • Usage of __slots__?
  • What are the uses of the exec command in shell scripts?
  • How to determine the current shell I'm working on
  • ExpressJS How to structure an application?
  • pandas: best way to select all columns whose names…
  • How can I create a Promise in TypeScript from a union type
  • What are the undocumented features and limitations…
  • Why cat does not work with parameter -0 in xargs?
  • Is this request generated by EF Core buggy or is it my code?
  • integrating disqus with emberjs only works on first…
  • Python, Username and Password with 3 attempts
  • Problems with local variable scope. How to solve it?
  • How can I exclude all "permission denied" messages…
  • How/When does Execute Shell mark a build as failure…
  • How do SO_REUSEADDR and SO_REUSEPORT differ?
  • Running shell command and capturing the output
  • python 3.2 UnicodeEncodeError: 'charmap' codec can't…
  • How can I throw CHECKED exceptions from inside Java…
  • Callback functions in C++
  • Generate sequence of dates for given frequency as…
  • How can I switch to another branch in git?
  • Why does an SSH remote command get fewer environment…
  • Individual click handlers in v-for loop
  • How is Perl's @INC constructed? (aka What are all…
  • Where can I set environment variables that crontab will use?
  • How do I use the nohup command without getting nohup.out?
  • Understanding PrimeFaces process/update and JSF…
  • How can I fix MySQL error #1064?
  • Difference between variable declaration syntaxes in…
  • Proper .htaccess config for Next.js SSG
  • Check synchronously if file/directory exists in Node.js
  • Smart way to truncate long strings
  • How to add elements of a Java8 stream into an existing List
  • How can I manually compile a svelte component down…
  • Converting a Java Keystore into PEM Format
  • Getting started with Haskell
  • Creating an instance of class
  • Create directories using make file
  • What's a concise way to check that environment…
  • What does "Fatal error: Unexpectedly found nil while…
  • How can I pass a wct test while rearranging children…
  • Liveness Probe through C# code giving error
  • How to make a SIMPLE C++ Makefile
  • Aurelia bundling issue with virtual directory
  • How can I find the product GUID of an installed MSI setup?
  • data.table vs dplyr: can one do something well the…
  • How to use shell commands in Makefile
  • Docker compose fails to start a service with an…
  • How can I initialize a MySQL database with schema in…
  • Command not found when using sudo
  • Vue: How do you add E2E tests after not including…
  • Get exit code for command in bash/ksh
  • How to use Servlets and Ajax?
  • Class vs. static method in JavaScript
  • Start redis-server with config file
  • The mysql extension is deprecated and will be…
  • Actual meaning of 'shell=True' in subprocess
  • How to implement the factory method pattern in C++ correctly
  • How does PHP 'foreach' actually work?
  • Deleting folders in python recursively
  • What are the currently supported CSS selectors…
  • Understanding repr( ) function in Python
  • What is a NullReferenceException, and how do I fix it?
  • Simplest way to create Unix-like continuous pipeline…
  • How to mount host volumes into docker containers in…
  • First echo missing when using bash -c over SSH
  • Bash Script : what does #!/bin/bash mean?
  • Check if a file is executable
  • Equals implementation with override Equals,…
  • Apache server keeps crashing, "caught SIGTERM,…
  • Identifying and solving…
  • IOException: The process cannot access the file…
  • How to suppress output of exception while running…
  • Python copy files to a new directory and rename if…
  • The definitive guide to form-based website authentication
  • A variable modified inside a while loop is not remembered
  • Create Directory if it doesn't exist with Ruby
  • Runing vue/cli app under docker simple index.html is opened
  • How do I reference a styled-component that is a…
  • Polymer events from distant nodes
  • Why use Select Top 100 Percent?
  • Fastest way to iterate over all the chars in a String
  • Rails wrong number of arguments error when…
  • Can a shell script set environment variables of the…
  • Is there a way to make mv create the directory to be…
  • JavaScript unit test tools for TDD
  • CMake error at CMakeLists.txt:30 (project): No…
  • What is an IndexOutOfRangeException /…
  • Ukkonen's suffix tree algorithm in plain English
  • Why does C++ code for testing the Collatz conjecture…
  • AppCompat v7 r21 returning error in values.xml?
  • Python subprocess with apostrophes, removes them
  • Logging best practices

Disclaimer: This content is shared under creative common license cc-by-sa 3.0. It is generated from StackExchange Website Network.

Post navigation

Previous Post:

How do I kill the process currently using a port on localhost in Windows?

Next Post:

Why is it string.join(list) instead of list.join(string)?

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

.net ajax android angular arrays aurelia backbone.js bash c++ css dataframe ember-data ember.js excel git html ios java javascript jquery json laravel linux list mysql next.js node.js pandas php polymer polymer-1.0 python python-3.x r reactjs regex sql sql-server string svelte typescript vue-component vue.js vuejs2 vuetify.js

  • you shouldn’t need to use z-index
  • No column in target database, but getting “The schema update is terminating because data loss might occur”
  • Angular – expected call-signature: ‘changePassword’ to have a typedeftslint(typedef)
  • trying to implement NativeAdFactory imports deprecated method by default in flutter java project
  • What should I use to get an attribute out of my foreign table in Laravel?
© 2022 Fix Code Error