Tuesday, 7 July 2020

HowTo: Check If a Directory Exists In a Shell Script

How do I check if a directory exists in a shell script under Linux or Unix like operating systems? How can I check if a directory exists in a shell script on Unix-like system?

To check if a directory exists in a shell script and is a directory use the following syntax:
[ -d "/path/to/dir" ] && echo "Directory /path/to/dir exists."
OR
[ ! -d "/path/to/dir" ] && echo "Directory /path/to/dir DOES NOT exists."

No comments:

Post a Comment

How to check if a file contains a specific string using Bash

 In case if you want to check whether file does not contain a specific string, you can do it as follows. if ! grep -q SomeString "$File...