添加链接
link管理
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接

Determine whether a file exists or not in Bash

David Y.

The Problem

How can I determine whether a file exists in Bash? Conversely, how can I test that it does not exist?

The Solution

We can check whether a file exists using the test command-line utility:

if test -f /path/to/file; then echo "File exists."

To check whether a file does not exist, we can negate the condition with the NOT logical operator, ! :

if ! test -f /path/to/file; then echo "File does not exist."