Unix Bash Shell Programming If Directory Exists

How do I check if a directory exists in a Bash shell script?

To check if a directory exists:

if [ -d "$DIRECTORY" ]; then
echo "$DIRECTORY does exist."
fi

To check if a directory does not exist:

if [ ! -d "$DIRECTORY" ]; then
echo "$DIRECTORY does not exist."
fi

However, as Jon Ericson points out, subsequent commands may not work as intended if you do not take into account that a symbolic link to a directory will also pass this check.
E.g. running this:

ln -s "$ACTUAL_DIR" "$SYMLINK"
if [ -d "$SYMLINK" ]; then
rmdir "$SYMLINK"
fi

Will produce the error message:

rmdir: failed to remove `symlink': Not a directory

So symbolic links may have to be treated differently, if subsequent commands expect directories:

if [ -d "$LINK_OR_DIR" ]; then 
if [ -L "$LINK_OR_DIR" ]; then
# It is a symlink!
# Symbolic link specific commands go here.
rm "$LINK_OR_DIR"
else
# It's a directory!
# Directory command goes here.
rmdir "$LINK_OR_DIR"
fi
fi

Take particular note of the double-quotes used to wrap the variables. The reason for this is explained by 8jean in another answer.

If the variables contain spaces or other unusual characters it will probably cause the script to fail.

Unix Bash Shell Programming if directory exists

Try:

if [ -d ~/tmp/"$sd" ]; then

or:

if [ -d "$HOME/tmp/$sd" ]; then

Quoting prevents expansion of ~ into your home directory.

Bash: how to check if directory exists?

Using Bash, this will give you the behavior you're looking for:

if [[ -d /opt/directory1 ]] || [[ -d /opt/directory2 ]] ; then
echo "SUCCESS"
else
echo "FAIL"
fi

Note the usage of -d on both conditions.

Shell script to check dir directory if it exists then change the path, if not then create dir with that name and also check for file name not exists

To check if a directory exists you can use the below test:

[ ! -d "$DIRNAME" ]

The complete script:

if [ ! -d "${DIRNAME}" ]; then
mkdir ${DIRNAME}
fi
cd ${DIRNAME}

Another solution could be create however the directory with -p option that does not return error if it exists:

mkdir -p ${DIRNAME}
cd ${DIRNAME}

check if directory exists and delete in one command unix

Assuming $WORKING_DIR is set to the directory... this one-liner should do it:

if [ -d "$WORKING_DIR" ]; then rm -Rf $WORKING_DIR; fi

(otherwise just replace with your directory)

For loop to find out if directory exists in unix

Try this:

while [ -n "$1" ]
do
dir="$1"
msg="$2"
if [ -d "$dir" ]; then
echo "$msg dir FOUND"
else
echo "$msg dir NOT FOUND"
fi
shift 2
done

shift <n> command simply shifts left positional parameters passed to the script of n positions.
For example if you call a script with:

./myscript 1 2 3 4

$1 is "1" and $2 is "2"

but if you shift 2 then $1 is "3" and $2 is "4".

In this way the loop consumes 2 parameters per cycle until $1 parameter is an empty string ( -n "$1").

while condition can be written more elegantly as:

while (( $# ))

obtaining the same result.

You can also check for the second parameter (while [ -n "$2" ]) but this changes the behavior when user provides an odd number of parameters:

  • in the first case last directory will be checked but you'll have a strange message because $msg il empty
  • il the second case you'll not have strange messages, but last directory will silently not be checked

Better test parameters at the beginning:

if (( $# % 2 ))
then
echo "Provide an even number of parameters"
exit 1
fi

How to check if a file exists in a shell script

You're missing a required space between the bracket and -e:

#!/bin/bash
if [ -e x.txt ]
then
echo "ok"
else
echo "nok"
fi

How do I check whether a file or file directory exist in bash?

Checking file and/or directory existence

To check whether a file exists in bash, you use the -f operator. For directories, use -d. Example usage:

$ mkdir dir
$ [ -d dir ] && echo exists!
exists!
$ rmdir dir
$ [ -d dir ] && echo exists!
$ touch file
$ [ -f file ] || echo "doesn't exist..."
$ rm file
$ [ -f file ] || echo "doesn't exist..."
doesn't exist...

For more information simply execute man test.

A note on -e, this test operator checks whether a file exists. While this may seem like a good choice, it's better to use -f which will return false if the file isn't a regular file. /dev/null for example is a file but nor a regular file. Having the check return true is undesired in this case.

A note on variables

Be sure to quote variables too, once you have a space or any other special character contained in a variable it can have undesired side effects. So when you test for existence of files and directories, wrap the file/dir in double quotes. Something like [ -f "/path/to/some/${dir}/" ] will work while the following would fail if there is a space in dir: [ -f /path/to/some/${dir}/ ].

Fixing the syntax error

You are experiencing a syntax error in the control statements. A bash if clause is structured as following:

if ...; then
...
fi

Or optional with an else clause:

if ...; then
...
else
...
fi

You cannot omit the then clause. If you wish to only use the else clause you should negate the condition. Resulting in following code:

if [ ! -f "/usr/share/icons/$j/scalable" ]; then
mkdir "/usr/share/icons/$j/scalable/"
fi

Here we add an exclamation point (!) to flip the expression's evaluation. If the expression evaluates to true, the same expression preceded by ! will return false and the other way around.



Related Topics



Leave a reply



Submit