Bash code snippet to check if file exists
FILE=/etc/passwd if [ -f $FILE ] ; then echo "$FILE exists and is regular file" else echo "$FILE either does not exist or is not regular file" fi
Outcome:
/etc/passwd exists and is regular file
Bash code snippet to check if directory exists
DIR=/etc if [ -d $DIR ] ; then echo "$DIR exists and is a dir" else echo "$DIR does not exist or is not a dir" fi
Outcome:
/etc exists and is a dir
Bash code snippet to check if file or directory exists
FILE_OR_DIR=/etc if [ -e $FILE_OR_DIR ] ; then echo "$FILE_OR_DIR exists" else echo "$FILE_OR_DIR does not exist" fi
Outcome:
/etc exists