Syntax

Bash Check if a File Exists Syntax

In Bash, check if a file exists: tests for a file or directory — [[ -f file ]] [[ -d dir ]]

Quick Answer

Syntax
[[ -f file ]] [[ -d dir ]]
BashSyntax

Check if a File Exists

Code example
[[ -f file ]] [[ -d dir ]]

Tests for a file or directory.

Learn More

How to check if a file exists in Bash

  1. Open your Bash file or editor.
  2. Copy the example, then replace its sample names and values for your code.

Bash Check if a File Exists Syntax

Use [[ -f file ]] [[ -d dir ]].

Related Syntax References

Frequently asked questions

What is the Bash syntax for check if a file exists?

Use this pattern: [[ -f file ]] [[ -d dir ]]

Can I copy this Bash code example?

Yes. Use Copy code and adapt the example to your program.

Is this Bash syntax different on Mac or Windows?

No. The language syntax is the same across operating systems.

Official source · How we verify