How to Check If a File Exist in Bash

Itacen Sabacok | Jul 2, 2022

With using command line;

1test -f /etc/os-release && echo "file exist"
2
3[ -f /etc/os-release ] && echo "file exist"
4
5[[ -f /etc/os-release ]] && echo "file exist"

With in a Bash script;

1FILE=/etc/os-release
2if [ -f "$FILE" ]; then
3    echo "$FILE exists."
4else 
5    echo "$FILE does not exist."
6fi