我正在编写一个脚本,该脚本检查特定目录中是否存在以特定单词开头的任何子目录。
到目前为止,这是我的脚本:
#!/bin/bash
function checkDirectory() {
themeDirectory="/usr/share/themes"
iconDirectory="/usr/share/icons"
# I don't know what to put for the regex.
regex=
if [ -d "$themeDirectory/$regex" && -d "$iconDirectory/$regex" ]; then
echo "Directories exist."
else
echo "Directories don't exist."
fi
}
因此,您将regex
如何检查特定目录中是否包含以特定单词开头的文件夹?
如果您只想查找与给定模式/前缀匹配的目录,我想您可以使用find
:
find /target/directory -type d -name "prefix*"
或者,如果您只想要直接子目录:
find /target/directory -maxdepth 1 -type d -name "prefix*"
当然,-regex
如果您需要实际的正则表达式匹配项,那也可以。(注意:我不记得-maxdepth是否是个gnu-ism。)
(更新)对,您想要一个if语句。Find总是返回零,因此我们不能使用返回值来检查是否找到了任何东西(与grep不同)。但是我们可以例如数线。通过管道传递输出wc
以获取计数,然后查看其是否不为零:
if [ $(find /target/directory -type d -name "prefix*" | wc -l ) != "0" ] ; then
echo something was found
else
echo nope, didn't find anything
fi
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句