Home  >  Article  >  Operation and Maintenance  >  How to use if in Linux to determine whether a directory exists

How to use if in Linux to determine whether a directory exists

angryTom
angryTomOriginal
2020-03-10 16:12:153225browse

How to use if in Linux to determine whether a directory exists

How to use if in Linux to determine whether a directory exists

The method is as follows:

1. Script How to use if to determine whether a directory exists

#! /bin/bash
if [ -d "c" ];then
    echo "目录c存在"
 else
     echo "目录不存在"
fi

Recommendation:Linux Tutorial Column

2. Simple writing method

#! /bin/bash
[ -d "c" ] && echo "目录c存在"
# 或者
[ -d "d" ] || echo "目录d不存在"

More judgment formats are as follows:

-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 如果文件是软链接,则为真

Related recommendations:

Linux video tutorial, learning address: https://www.php.cn /course/list/33.html

The above is the detailed content of How to use if in Linux to determine whether a directory exists. 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