1. Home
  2. Bash
  3. Examples
  4. Bash – Check If Directory Exists

Bash – Check If Directory Exists

Bash Program to Check if A Directory Exists

Q. How do I check if a directory exists or not?

Add the following code to a shell script to verify if defined directory exists. Then execute a set of statement if directory exists:

  • Check if a directory exists:
  • Check if a directory doesn’t exist:

Example

A sample shell script to navigate to a specific directory only if exists.

In most cases, where you are switching to directory which don’t exists. In that case, the script will exit with an error. So its good to create directory first and then switch to directory.