📚Cheatsheets

Cheatsheet collection for go, rust, python, shell and javascript.

Check if folder exists or Not

To check if a folder exists in Bash, you can use the test command to check if a directory exists and is a directory. The -d option is used to check for directories, and the -e option is used to check for the existence of any kind of file.

Here is an example of how you can check if a folder exists in Bash:

if test -d /path/to/folder; then
  # folder exists and is a directory
else
  # folder does not exist or is not a directory
fi

Alternatively, you can also use the [ -d /path/to/folder ] syntax, which is equivalent to the test command shown above.

Here is an example of how you can use this syntax to check if a folder exists:

if [ -d /path/to/folder ]; then
  # folder exists and is a directory
else
  # folder does not exist or is not a directory
fi

In both examples, the if statement checks if the /path/to/folder directory exists and is a directory. If it does, then the code in the then block will be executed. Otherwise, the code in the else block will be executed.