Saturday, August 13, 2022

Bash - How to Check if a File or Directory Exists in Mac/Unix

 


How to Check if a File or Directory Exists using Bash?


Using IF statement

FILENAME=/etc/conf.yml
if [ -f "$FILENAME" ]; then
echo "$FILENAME exists."
else
echo "$FILENAME does not exist."
fi

Without using IF statement

[[ -f /etc/conf.yml ]] && echo "$FILENAME exists."

No comments:

Post a Comment