Home>Article>Operation and Maintenance> How to find whether a directory or file exists in Linux

How to find whether a directory or file exists in Linux

WBOY
WBOY Original
2022-04-07 12:05:15 15893browse

Method: 1. Use the find command, the syntax is "find directory or file search rules"; 2. Use the shell script, the syntax is "if [-e directory or file]; then echo "file exists" else echo "File "fi" does not exist."

How to find whether a directory or file exists in Linux

#The operating environment of this tutorial: linux7.3 system, Dell G3 computer.

How to find whether a directory or file exists in Linux

Two methods: find command or shell script.

1. Find command

(1) find is a common method for finding files under Linux.

(2) find syntax:

find [指定查找目录] [查找规则] [查找完后执行的action]

(3) For example: find /tmp -name wa* -type l, it is to find a symbolic link named starting with wa under /tmp. document. To find it means to exist.

2. Shell script

(1) Automatic identification is often required during automatic file processing. The following script determines whether test.log exists and displays it if it exists. The file exists, otherwise it displays that the file does not exist.

(2) Example: Edit a script to determine whether the file exists.

vi t.sh #!/bin/bash if [ -e /temp/test.log ];then //这里是判断语句,-e表示进行比较结果为真则存在 echo "文件存在" else echo "文件不存在" fi

Examples are as follows:

How to find whether a directory or file exists in Linux

Related recommendations: "Linux Video Tutorial"

The above is the detailed content of How to find whether a directory or 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