Search
 
SCRIPT & CODE EXAMPLE
 

SHELL

shell script to check the directory exists

Directory="/opt"
if [ -d "$Directory" ];
then
	echo -e "it's exits
"
fi
### To check if it's not exists
if [ ! -d "$Directory" ];
then 
	echo -e "It's not there
"
fi
Comment

linux command if directory exists

DIR="/etc/httpd/"
if [ -d "$DIR" ]; then
  ### Take action if $DIR exists ###
  echo "Installing config files in ${DIR}..."
else
  ###  Control will jump here if $DIR does NOT exists ###
  echo "Error: ${DIR} not found. Can not continue."
  exit 1
fi
Comment

Check is directory exist shell

DIR="/etc/httpd/"
if [ -d "$DIR" ]; then
  # Take action if $DIR exists. #
  echo "Installing config files in ${DIR}..."
fi
Comment

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

To check if a directory exists in a shell script, you can use the following:

if [ -d "$DIRECTORY" ]; then
  # Control will enter here if $DIRECTORY exists.
fi
Or to check if a directory doesn't exist:

if [ ! -d "$DIRECTORY" ]; then
  # Control will enter here if $DIRECTORY doesn't 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.

Comment

PREVIOUS NEXT
Code Example
Shell :: install workbench on ubuntu 
Shell :: powershell get input from user 
Shell :: install pip in ubuntu 
Shell :: get git username and email 
Shell :: nvm install lts 
Shell :: public class FileProvider extends android.support.v4.content.FileProvider 
Shell :: install pavucontrol ubuntu 20.04 
Shell :: show mac address in ubuntu 
Shell :: how to install and enable docker on command line with EC2 instance 
Shell :: pip reinstall 
Shell :: install aws cli 2 yum 
Shell :: open folder from terminal in kali 
Shell :: check services running on port linux 
Shell :: how to install pip on mac 
Shell :: bash: cmake: command not found 
Shell :: OSError: [Errno 24] inotify instance limit reached 
Shell :: brew install jdk 8 
Shell :: how to kill tasks using grep 
Shell :: install ionic globally 
Shell :: adb command to open deeplink 
Shell :: change git commit message 
Shell :: reinstal apache2 ubuntu 
Shell :: installing eslint globally 
Shell :: install webmin ubuntu 20.04 
Shell :: gitlab ci allow failure 
Shell :: ignore file ownership changes git 
Shell :: vim strip trailing whitespace 
Shell :: debian install vim 
Shell :: wget clone entire website 
Shell :: homebrew restart redis 
ADD CONTENT
Topic
Content
Source link
Name
7+3 =