Timeline for How do I check if a directory exists or not in a Bash shell script?
Current License: CC BY-SA 3.0
5 events
| when toggle format | what | by | license | comment | |
|---|---|---|---|---|---|
| Feb 19, 2021 at 13:25 | comment | added | zerzevul |
ls returning non-zero DOES NOT mean that the directory exists. The code should be: ls $DIR RETURN_CODE=$? if [ "$RETURN_CODE" != "0" ]; then echo "Cannot access directory $DIR (does not exist or no permission or something else altogether)!" exit $RETURN_CODE; fi echo "Directory $DIR already exists..."
|
|
| Jan 25, 2021 at 14:30 | review | Suggested edits | |||
| Jan 29, 2021 at 14:24 | |||||
| Jan 8, 2014 at 10:51 | comment | added | derFunk |
Alternatively you can use this shorter version: if ! ls $DIR 2>/dev/null; then echo "$DIR does not exist!"; fi
|
|
| Jan 8, 2014 at 10:38 | history | edited | derFunk | CC BY-SA 3.0 |
added 2 characters in body
|
| Jan 7, 2014 at 17:59 | history | answered | derFunk | CC BY-SA 3.0 |