Welcome!

Unlock your personalized experience.
Sign Up
Tag

Check if file exists Linux Articles, Guides & Tutorials

1 posts

How to Check if a File or Directory Exists in Bash Scripts

In Bash, use [[ -f "$path" ]] to check that a regular file exists, [[ -d "$path" ]] for a directory, and [[ -e "$path" ]] for anything at that path. Add ! to test that it does not exist. Always quote variables, use [ ] instead of [[ ]] for portable POSIX sh scripts, and remember that a check can become out of date before you act on it, so handle errors from the actual operation too.