Linux Shell Script|mkdir: cannot create directory ‘DIRECTORY NAME’: File exists|Error cause and how to deal with it|bash shell


When running a shell script on Linux, a mkdir: cannot create directory ‘DIRECTORY NAME’: File exists error occurred. This section describes the cause of the error and how to deal with it.


Phenomenon

When running the shell script, the following error message was displayed.

・Error message

mkdir: cannot create directory ‘DIRECTORY NAME’: File exists

Cause

An error occurred when trying to use the mkdir command again to create a directory with the same name as an already existing directory. For example, if the shell "test.sh" below is executed when the "work" directory already exists in the current directory, a similar error will occur.



test.sh
#!/bin/bash

mkdir ./work


Execution result

mkdir: cannot create directory ‘work’: File exists

How to respond

Use the "-p" option with the mkdir command. When you run the mkdir command using the "-p" option, if the directory already exists, no error occurs and the directory is not created again. It will also create parent directories if necessary. For example, if you run the shell "test.sh" below when the "work" directory already exists in the current directory, no error will occur.

test.sh
#!/bin/bash

mkdir -p ./work




スポンサーリンク

0 件のコメント :

コメントを投稿