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 shell check if directory does not exists

if [ ! -d directory ]; then
  mkdir directory
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 :: manjaro how to erase a usb 
Shell :: gcsfuse allow_other 
Shell :: libdnet 
Shell :: automated install windows admin center 
Shell :: lhapüasd 
Shell :: apche superset installation 
Shell :: bash get length of rows 
Shell :: cordova ios emulators 
Shell :: firewall in ubuntu 18.04 
Shell :: find and execute a command 
Shell :: replace text with sed 
Shell :: how to uninstall lxd ubuntu 20.04 
Shell :: gh login 
Shell :: aws security groups vs ufw 
Shell :: chagne remote origin git 
Shell :: git commit convention 
Shell :: shell count number of lines 
Shell :: database metasploit kali linux 
Shell :: linux test port 
Shell :: mysql inline pass cli 
Shell :: linux set multiline variable 
Shell :: find signing key certificate 
Shell :: bash location of substring match within string 
Shell :: kill process at a port ubuntu 
Shell :: ubuntu server does not run scripts 
Shell :: start redis server 
Shell :: debian kill open port 
Shell :: remove or undo last merge git 
Shell :: jupyter notebook allow root 
Shell :: linux check apt repositories 
ADD CONTENT
Topic
Content
Source link
Name
6+2 =