Skip to content
Advertisement

If not exists, create a folder – Error in makefile

I searched over the internet for a command to create a folder if it doesn’t exist. I found it and put in my makefile

.DEFAULT_GOAL := all
folder := "myfolder"

createfolder:
    [ ! -d ${folder} ] && mkdir -p ${folder}

nextstep:
    echo "Got here!"

all: createfolder nextstep

When the folder doesn’t exist it’s created correctly. But I get an error if the folder already exists.

$ ls
makefile
$ make
[ ! -d "myfolder" ] && mkdir -p "myfolder"
echo "Got here!"
Got here!
$ ls
makefile  myfolder
$ make
[ ! -d "myfolder" ] && mkdir -p "myfolder"
make: *** [makefile:5: createfolder] Error 1

I don’t get why the command would give an error if the condition [ ! -d "myfolder" ] is before the mkdir and it shouldn’t even execute the second command.

How can I solve it?

Advertisement

Answer

this is what make is for.

${folder}: 
        mkdir -p ${folder}
User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement