1. Home
  2. Bash
  3. Examples
  4. Bash – Check If A File Exists

Bash – Check If A File Exists

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.

Was this article helpful to you? Yes 1 No
Advertisement