32

Given a file path (e.g. /src/com/mot), how can I check whether mot exists, and create it if it doesn't using Linux or shell scripting??

Matt
  • 74,352
  • 26
  • 153
  • 180
Vivek
  • 10,978
  • 14
  • 48
  • 66
  • 3
    Does this answer your question? [How can I check if a directory exists in a Bash shell script?](https://stackoverflow.com/questions/59838/how-can-i-check-if-a-directory-exists-in-a-bash-shell-script) – Josh Correia Dec 09 '20 at 18:02

6 Answers6

31

With bash/sh/ksh, you can do:

if [ ! -d /directory/to/check ]; then
    mkdir -p /directory/toc/check
fi

For files, replace -d with -f, then you can do whatever operations you need on the non-existant file.

Chris J
  • 30,688
  • 6
  • 69
  • 111
  • 7
    You don't actually need the check. `mkdir -p` doesn't act on an existing directory. – thiton Jan 09 '12 at 15:11
  • 2
    That's a fair point. I'll leave it in though as if gives the OP the framework in case they wants to do other things before the `mkdir`. – Chris J Jan 09 '12 at 15:13
  • 1
    Note that this gives a race condition in the case that the directory is created between the first and second line. – Sjoerd Jan 09 '12 at 15:14
  • 2
    @Sjoerd Indeed, but that race condition (although with a much smaller window of probability) will also occur if you just use `mkdir -p` (unless some filesystem implements, and exposes, an atomic check-or-create function call). At least in the code above the mkdir call will not fail if invoked spuriously for an existing directory (because of the `-p` option provided anyway). – Christian.K Jan 09 '12 at 15:20
  • @Christian If you want an atomic check-or-create function, look no further than mkdir(2) – William Pursell Jan 09 '12 at 15:36
  • @WilliamPursell Sorry, but I don't get that. According to [this](http://pubs.opengroup.org/onlinepubs/009695399/functions/mkdir.html) mkdir(2) will fail with `EEXIST` if the specified directory exists. Not the behavior of `mkdir(1) -p`. At least on Solaris (I don't know about Linux) the behavior of the `-p` option is implemented using a [library routine](http://src.opensolaris.org/source/xref/onnv/onnv-gate/usr/src/lib/libgen/common/mkdirp.c#52) which iterates using `access()` and `mkdir()` for each path component - not atomic. I might be totally off regarding your comment however ;-) – Christian.K Jan 09 '12 at 19:07
  • @Christian: mkdir (without -p) is often used in shell scripts as a way to acquire a lock. I read sjoerd's comment as applying to the case without -p – William Pursell Jan 09 '12 at 19:47
10

Check for directory exists

if [ -d "$DIRPATH" ]; then
    # Add code logic here 
fi

Check for directory does not exist

if [ ! -d "$DIRPATH" ]; then
    # Add code logic here
fi
parapura rajkumar
  • 24,045
  • 1
  • 55
  • 85
9

mkdir -p creates the directory without giving an error if it already exists.

Sjoerd
  • 74,049
  • 16
  • 131
  • 175
7

Well, if you only check for the directory to create it if it does not exist, you might as well just use:

mkdir -p /src/com/mot

mkdir -p will create the directory if it does not exist, otherwise does nothing.

Christian.K
  • 47,778
  • 10
  • 99
  • 143
6
test -d /src/com/mot || mkdir /src/com/mot
Eugen Rieck
  • 64,175
  • 10
  • 70
  • 92
3

This is baisc, but I think it works. You'll have to set a few variables if you're looking to have a dynamic list to cycle through and check.

if [ -d /src/com/mot ];
then
    echo Directory found
else
    mkdir /src/com/mot
fi

Hope that's what you were looking for...

Silvertiger
  • 1,680
  • 2
  • 19
  • 32
  • You're missing the ';' after the closing ']' of the if-clause. – Christian.K Jan 09 '12 at 15:16
  • @Chirstian The ';' is not needed after the ']', and the ']' is not a closing bracket. In the case of the command '[', the trailing ']' is just an argument to that command. The newline serves the same purpose as the semi-colon. – William Pursell Jan 09 '12 at 15:30
  • @WilliamPursell My comment was regarding the first revision of the question, which did not contain a newline after the ']' after the "test" command (or "brackets", sorry for missnaming it), in which case the ';' is required before the `then`, isn't it? – Christian.K Jan 09 '12 at 19:14
  • @christian, yes, either a newline or a ; is required. When I saw the question, the newline was in place. – William Pursell Jan 09 '12 at 19:44