Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Shell Script - Make directory if it doesn't exist

I want to enter the name of a directory and check if it exists. If it doesn't exist I want to create but I get the error mkdir: cannot create directory'./' File exists

My code says that the file exists even though it doesn't. What am I doing wrong?

echo "Enter directory name"
read dirname

if [[ ! -d "$dirname" ]]
then
    if [ -L $dirname]
then
    echo "File doesn't exist. Creating now"
    mkdir ./$dirname
    echo "File created"
    else
        echo "File exists"
    fi
fi
like image 781
Pizzaman Avatar asked Apr 07 '14 11:04

Pizzaman


People also ask

How do you create a directory if not exists in Shell?

You can either use an if statement to check if the directory exists or not. If it does not exits, then create the directory. You can directory use mkdir with -p option to create a directory. It will check if the directory is not available it will.

Does cp create directory if not exists?

The cp command will abort if the target directory doesn't exist. Sometimes, this brings inconvenience when we work with files in the Linux command line. In this article, we've addressed three techniques to create the non-existing target directory automatically during file copying: mkdir -p && cp.

How do I ignore mkdir if exists?

This indicates that if a directory already exists, the mkdir command will not try to create or modify the already existing directory in any way. But it also shows an error which is not what you always want especially if you are writing a bash script. To prevent this error, use the -p flag along with mkdir command.

How do I create a directory in shell script?

Create a New Directory ( mkdir ) The first step in creating a new directory is to navigate to the directory that you would like to be the parent directory to this new directory using cd . Then, use the command mkdir followed by the name you would like to give the new directory (e.g. mkdir directory-name ).


2 Answers

if [ -L $dirname]

Look at the error message produced by this line: “[: missing `]'” or some such (depending on which shell you're using). You need a space inside the brackets. You also need double quotes around the variable expansion unless you use double brackets; you can either learn the rules, or use a simple rule: always use double quotes around variable substitution and command substitution"$foo", "$(foo)".

if [ -L "$dirname" ]

Then there's a logic error: you're creating the directory only if there is a symbolic link which does not point to a directory. You presumably meant to have a negation in there.

Don't forget that the directory might be created while your script is running, so it's possible that your check will show that the directory doesn't exist but the directory will exist when you try to create it. Never do “check then do”, always do “do and catch failure”.

The right way to create a directory if it doesn't exist is

mkdir -p -- "$dirname"

(The double quotes in case $dirname contains whitespace or globbing characters, the -- in case it starts with -.)

like image 90
Gilles 'SO- stop being evil' Avatar answered Oct 15 '22 08:10

Gilles 'SO- stop being evil'


Try this code:

echo "Enter directory name"
read dirname

if [ ! -d "$dirname" ]
then
    echo "File doesn't exist. Creating now"
    mkdir ./$dirname
    echo "File created"
else
    echo "File exists"
fi

Output Log:

Chitta:~/cpp/shell$ ls
dir.sh

Chitta:~/cpp/shell$ sh dir.sh
Enter directory name
New1
File doesn't exist. Creating now
File created

chitta:~/cpp/shell$ ls
New1  dir.sh

Chitta:~/cpp/shell$ sh dir.sh
Enter directory name
New1
File exists

Chitta:~/cpp/shell$ sh dir.sh
Enter directory name
New2
File doesn't exist. Creating now
File created

Chitta:~/cpp/shell$ ls
New1  New2  dir.sh
like image 30
Chittaranjan Sethi Avatar answered Oct 15 '22 06:10

Chittaranjan Sethi