[英]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
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.