What Does -D Value in an If Expression in Shell?

I am trying to understand a part of search function written in a piece of code below is the expression mentioned.

if [ -d $1] 
then
<action block>
4

2 Answers

You can use it to determine if a directory exist like

if [ -d "$DIR" ]; then
   # code
fi
1

The -d is used to find if a given directory is exists or not. If it is exists the condition will be true. if not the condition will be false.

Where as -f is used to find if a given file is exists or not. If it is exists the condition will be true. if not the condition will be false

if [ -d "$dirname" ]
then
  echo "directory is present"
else
  echo "directory is not present"
fi

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Robert Thorne

Robert Thorne

Automotive & Future Transportation Editor

Robert Thorne covers electric vehicle innovations, autonomous driving systems, global mobility trends, and automotive engineering developments.

Share this article
Twitter Facebook Pinterest