Home  >  Article  >  Operation and Maintenance  >  Determine whether the file exists in linux

Determine whether the file exists in linux

(*-*)浩
(*-*)浩Original
2019-11-08 09:22:513955browse

Determine whether the file exists in linux

#In Linux shell, -f is generally used to determine whether the file exists; -d is used to determine the directory.

[[ -f 文件名 ]] && echo yes

If yes is printed, it means that the file can be detected by the current user        (Recommended learning: linux operation and maintenance)

-e filename 如果 filename存在,则为真
-d filename 如果 filename为目录,则为真
-f filename 如果 filename为常规文件,则为真
-L filename 如果 filename为符号链接,则为真
-r filename 如果 filename可读,则为真
-w filename 如果 filename可写,则为真
-x filename 如果 filename可执行,则为真
-s filename 如果文件长度不为0,则为真
-h filename 如果文件是软链接,则为真

Common examples

If a file exists, delete it

if [ -f trials ]; then rm ${result_path}trials; fi

If there is no folder, create it

if [ ! -d $result_name ];then
      mkdir -p $result_name
fi

The above is the detailed content of Determine whether the file exists in linux. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn