检查文件夹是否包含glob [英] Check if folder contains glob

查看:158
本文介绍了检查文件夹是否包含glob的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的Mac上,我试图找出一种方法来检查服务器上已装入的卷,以查看目录是否通过将在launchd中设置为时间间隔的shell脚本接收到日志文件./p>

根据我的搜索记录,历史上我曾经使用过:

$DIR="/path/to/file"
THEFILES=(`find ./ -maxdepth 1 -name "*.log"`)
if [ ${#THEFILES[@]} -gt 0 ]; then 
    echo "exists"
else
    echo "nothing"
fi

如果将shell脚本放置在该特定目录中,并且文件存在.但是,当我将脚本移到该目录之外并尝试:

THEFILES=(`find ./ -maxdepth 1 -name "*.log"`)
cd $DIR
if [ ${#THEFILES[@]} -gt 0 ]; then 
    echo "exists"
else
    echo "nothing"
fi

我得到nothing的不变回报.我认为可能与深度有关,因此我将-maxdepth 1更改为-maxdepth 0,但仍然得到nothing.通过搜索,我遇到了"检查目录中是否存在某种文件类型/扩展名"并尝试:

THEFILES=$(ls "$DIR/.log" 2> /dev/null | wc -l)
echo $THEFILES

,但是我返回了常量0.当我进一步搜索时,遇到"从shell脚本检查目录中是否包含文件",并尝试使用find与:

THEFILES=$(find "$DIR" -type f -regex '*.log')
cd $DIR
if [ ${#THEFILES[@]} -gt 0 ]; then 
    echo "exists"
else
    echo "nothing"
fi

,返回空白.当我尝试时:

if [ -n "$(ls -A $DIR)" ]; then
    echo "exists"
else
    echo "nothing"
fi

我得到一个空白终端.在此 answer 上,我的Mac上没有pruneshopt.因此,如何检查已挂载服务器的目录,以查看是否存在具有特定扩展名的特定文件,而该文件不会从隐藏文件中返回错误信息?

每条评论,我都尝试通过以下方式消除深度:

THEFILES=$(find ./ -name "*.log")

但是我得到一个空白的返回,但是如果我在其中放一个.log文件,它会运行,但是我不明白为什么除非考虑隐藏文件,否则为什么不返回nothing.感谢 l'L ,我了解到-prunefind的实用程序中,但是当我尝试时:

if [ -n "$(find $DIR -prune -empty -type d)" ]; then

当存在一个LOG文件时,我得到的常量返回值为nothing.

您提出的每个答案都确实可以正常工作!

我没有使用-maxdepth选项,如果仅检查$DIR下的文件而不是其子目录对您很重要,请随时将-maxdepth 1添加到对find的任何调用中./p>

使用 mklement0 指出的小写变量,以避免与环境变量发生冲突.

答案1

您设置了dir,但改用了./ ...

dir="/path/to/dir"
# Create an array of filenames
files=( $(find "$dir" -name "*.log") )
if [ ${#files[@]} -gt 0 ]; then # if the length of the array is more than 0
    echo "exists"
else
    echo "nothing"

答案2

您在运行查找后使用cd $dir ...

dir="path/to/dir"
cd "$dir"
files=( $(find -name "*.log") )
if [ ${#files[@]} -gt 0 ]; then
    echo "exists"
else
    echo "nothing"
fi

答案3

您在'.log'之前忘记了'*'...看看偷偷摸摸

dir="/path/to/dir"
# print all the files in "$DIR" that end with ".log"
# and count the number of lines
nbfiles=$(ls "$dir/"*.log 2> /dev/null | wc -l) # nbfiles is an integer
if [ $nbfiles -gt 0 ]; then
    echo "exists"
else
    echo "nothing
fi

答案4

find 的macOS手册页建议regex默认使用基本正则表达式:

-E

解释后跟-regex和-iregex的正则表达式 选项作为扩展(现代)正则表达式而不是 基本的正则表达式(BRE).

您在*之前缺少.cd $dir并不是必需的,因为您将$dir用作find的参数;并且您不会将find的输出存储在数组中,因此无法检查数组的长度.

# The output of find is stored as a string
files=$(find "/path/to/dir" -type f -regex '.*\.log')
if [ -n "$files" ]; then # Test that the string $files is not empty
    echo "exists"
else
    echo "nothing"
fi

On my Mac I am trying to figure out a way to check a mounted volume to a server to see if the directory receives a log file through a shell script that will be used in launchd set to a time interval.

From my searches and historically I've used:

$DIR="/path/to/file"
THEFILES=(`find ./ -maxdepth 1 -name "*.log"`)
if [ ${#THEFILES[@]} -gt 0 ]; then 
    echo "exists"
else
    echo "nothing"
fi

If the shell script is placed inside that particular directory and the files are present. However, when I move the script outside of that directory and try:

THEFILES=(`find ./ -maxdepth 1 -name "*.log"`)
cd $DIR
if [ ${#THEFILES[@]} -gt 0 ]; then 
    echo "exists"
else
    echo "nothing"
fi

I get a constant return of nothing. I thought it might be in regards to the depth so I changed -maxdepth 1 to -maxdepth 0 but I still get nothing. Through searching I ran across "Check whether a certain file type/extension exists in directory" and tried:

THEFILES=$(ls "$DIR/.log" 2> /dev/null | wc -l)
echo $THEFILES

but I'm returned a constant 0. When I searched further I ran across "Checking from shell script if a directory contains files" and tried a variation using find with:

THEFILES=$(find "$DIR" -type f -regex '*.log')
cd $DIR
if [ ${#THEFILES[@]} -gt 0 ]; then 
    echo "exists"
else
    echo "nothing"
fi

with a blank return. When I try:

if [ -n "$(ls -A $DIR)" ]; then
    echo "exists"
else
    echo "nothing"
fi

I get a blank terminal returned. On this answer I dont have prune or shopt on my Mac. So how I can I check a mounted server's directory to see if a particular file with a specific extension exists that will not give a false return from hidden files?

EDIT:

Per comment I tried removing the depth with:

THEFILES=$(find ./ -name "*.log")

but I get a blank return but if I drop a .log file in there it runs but I don't understand why else isn't returning nothing unless it's considering the hidden files. Thanks to l'L'l I learned -prune was in find's utility but when I try:

if [ -n "$(find $DIR -prune -empty -type d)" ]; then

I get a constant return of nothing when there is a LOG file present.

解决方案

Each of your proposed answer is really close to working!

I didn't use the -maxdepth option, if it is important to you to only check the files under $DIR but not its subdirectories, feel free to add -maxdepth 1 to any call to find.

Use lowercase variables as pointed by mklement0 to avoid conflicts with environment variables.

Answer 1

You set dir, but use ./ instead...

dir="/path/to/dir"
# Create an array of filenames
files=( $(find "$dir" -name "*.log") )
if [ ${#files[@]} -gt 0 ]; then # if the length of the array is more than 0
    echo "exists"
else
    echo "nothing"

Answer 2

You use cd $dir after running find...

dir="path/to/dir"
cd "$dir"
files=( $(find -name "*.log") )
if [ ${#files[@]} -gt 0 ]; then
    echo "exists"
else
    echo "nothing"
fi

Answer 3

you forgot a '*' before the '.log'... Have a look a the documentation for globbing

dir="/path/to/dir"
# print all the files in "$DIR" that end with ".log"
# and count the number of lines
nbfiles=$(ls "$dir/"*.log 2> /dev/null | wc -l) # nbfiles is an integer
if [ $nbfiles -gt 0 ]; then
    echo "exists"
else
    echo "nothing
fi

Answer 4

The macOS man page of find suggests that regex uses Basic Regular Expressions by default:

-E

Interpret regular expressions followed by -regex and -iregex options as extended (modern) regular expressions rather than basic regular expressions (BRE's).

You are missing a . before the * ; the cd $dir is not necessary since you pass $dir as an argument to find ; and you don't store the output of find in an array, so you can't check the length of the array.

# The output of find is stored as a string
files=$(find "/path/to/dir" -type f -regex '.*\.log')
if [ -n "$files" ]; then # Test that the string $files is not empty
    echo "exists"
else
    echo "nothing"
fi

这篇关于检查文件夹是否包含glob的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆