简体   繁体   English

如何在linux中检查目录是否存在。?

[英]How to check directory exist or not in linux.?

给定文件路径(例如/src/com/mot ),如何检查mot是否存在,如果不使用Linux或shell脚本,则创建它?

With bash/sh/ksh, you can do: 使用bash / sh / ksh,您可以:

if [ ! -d /directory/to/check ]; then
    mkdir -p /directory/toc/check
fi

For files, replace -d with -f , then you can do whatever operations you need on the non-existant file. 对于文件,将-d替换为-f ,然后您可以在不存在的文件上执行所需的任何操作。

mkdir -p创建目录而不会出现错误(如果已存在)。

test -d /src/com/mot || mkdir /src/com/mot

Well, if you only check for the directory to create it if it does not exist, you might as well just use: 好吧,如果你只检查创建它的目录(如果它不存在),你可以使用:

mkdir -p /src/com/mot

mkdir -p will create the directory if it does not exist, otherwise does nothing. 如果目录不存在, mkdir -p将创建该目录,否则不执行任何操作。

Check for directory exists 检查目录是否存在

if [ -d "$DIRPATH" ]; then
    # Add code logic here 
fi

Check for directory does not exist 检查目录是否不存在

if [ ! -d "$DIRPATH" ]; then
    # Add code logic here
fi

This is baisc, but I think it works. 这是baisc,但我认为它有效。 You'll have to set a few variables if you're looking to have a dynamic list to cycle through and check. 如果您希望有一个动态列表来循环检查,则必须设置一些变量。

if [ -d /src/com/mot ];
then
    echo Directory found
else
    mkdir /src/com/mot
fi

Hope that's what you were looking for... 希望这就是你要找的......

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM