3

What command checks if a directory exists in another path using shell script?

I searched about this, but all I could find was checking if a directory exists in "current" directory.

if [ -d "$DIRECTORY" ]; then
  # found "any" directory
fi

This doesn't satisfy when I want to know if "any" directory exist inside of another directory.

if [ -d "./PATH/$DIRECTORY" ]; then
  # found "any" directory in subdirectory
fi

Apparently this did not work. This still checks from current working directory, not the path I have given.

Any suggestions?

I would like to use this in if condition, so answering with keeping that form would be helpful.

EDIT

In shell script I added as following:

#!/bin/bash

ls -ld ./smth/world/
echo DIRECTORY=$DIRECTORY

if [ -d "./smth/$DIRECTORY/" ]; then
   echo hi
else
   echo nope
fi

in the current directory, I have a directory called "smth". Inside that "smth" I created a dummy directory "world"

EDIT #2

After I added the dot in front of path, I get the following:

drwxrwxr-x 2 (myname) (myname) 4096 Sep  5 14:39 ./smth/world/
DIRECTORY=
found

BUT after I delete the world directory inside of smth directory, still get "found".

2
  • Your question is unclear. Assuming that your variable DIRECTORY contains the word world. The expressions ` [ -d "/smth/$DIRECTORY" ]` or alternatively [[ -d /smth/$DIRECTORY ]] both test, whether the directory /smth/world (absolute path) exists. If your test doesn't show the desired result, I suggest that you - just for testing purpose do a ls -ld /smth/world and a echo DIRECTORY=$DIRECTORY right before the if statement. If your goal is to test something different, please rephrase your question. Commented Sep 5, 2016 at 9:02
  • I edited my question to what you told me, and I get the result as following Commented Sep 5, 2016 at 19:44

1 Answer 1

6

That's the other way. ./ if for a directory relative to the current directory, / if for a fully specified directory, i.e.:

If you provide a full path to the directory, you need to remove the dot:

if [ -d "/full/path/to/smth/$DIRECTORY" ]; then
  # found 
fi

If the PATH directory is relative to your current one, the dot must be kept:

if [ -d "./smth/$DIRECTORY" ]; then
  # found
fi

If the DIRECTORY variable is unset, the previous commands will check if there is a directory named PATH in the smth one, if the DIRECTORY variable is set, the test will only succeed if there is a subdirectory with that value under smth.

echo $DIRECTORY will tell you if the DIRECTORY variable is set.

Sign up to request clarification or add additional context in comments.

4 Comments

Wow I can't believe I didn't try that after almost 30 min. My rank is not high enough to upvote, but thank you so much!
Hmm... I don't know why but that always gives me false. I made a directory in "PATH" then wrote simple code "echo hi", but doesn't print anything.
Please edit your question and add the precise commands you ran and their output.
Have done so. There's really not much to see, but that's the code I used.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.