Bash Example to Check if A File Exists
Q. How do I check if a specified file exists or not?
Use -f switch with the if condition to check if a file exists. Then execute a set of statement if a file exists:
- Check if a file exists:
- Check if a file doesn’t exist:
Example
A sample shell script to remove a specific file if already exists.
Sometimes you are trying to access a file, which doesn’t exists. In that case, create file if not exists.