How To Check If a Directory Exists In a Shell Script

https://‮l.www‬autturi.com
How To Check If a Directory Exists In a Shell Script

To check if a directory exists in a shell script, you can use the test command with the -d option.

Here's an example of how to use the test command to check if a directory exists in a shell script:

if test -d /path/to/directory; then
  # Directory exists
else
  # Directory does not exist
fi

This will check if the directory at /path/to/directory exists. If the directory exists, the script will execute the commands in the then block. If the directory does not exist, the script will execute the commands in the else block.

You can also use the [ command, which is an alias for the test command, to check if a directory exists. For example:

if [ -d /path/to/directory ]; then
  # Directory exists
else
  # Directory does not exist
fi
Created Time:2017-10-28 21:38:50  Author:lautturi