bash 测试是否存在多个文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14765569/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Test if multiple files exist
提问by stefcud
How can i use test command for arbitrary number of files, passed in argument by regexp
我如何对任意数量的文件使用测试命令,通过正则表达式传入参数
for example:
例如:
test -f /var/log/apache2/access.log.* && echo "exists one or more files"
but mow print error: bash: test: too many arguments
但是修剪打印错误:bash:测试:参数太多
采纳答案by Hui Zheng
To avoid "too many arguments error", you need xargs. Unfortunately, test -f
doesn't support multiple files. The following one-liner should work:
为避免“参数过多错误”,您需要 xargs。不幸的是,test -f
不支持多个文件。以下单行应该工作:
for i in /var/log/apache2/access.log.*; do test -f "$i" && echo "exists one or more files" && break; done
BTW, /var/log/apache2/access.log.*
is called shell-globbing, not regexp, please check this: Confusion with shell-globbing wildcards and Regex.
顺便说一句,/var/log/apache2/access.log.*
被称为 shell-globbing,而不是 regexp,请检查这个:Confusion with shell-globbing wildcards and Regex。
回答by tdu
This solution seems to me more intuitive:
这个解决方案在我看来更直观:
if [ `ls -1 /var/log/apache2/access.log.* 2>/dev/null | wc -l ` -gt 0 ];
then
echo "ok"
else
echo "ko"
fi
回答by BenjaminBallard
If you wanted a list of files to process as a batch, as opposed to doing a separate action for each file, you could use find, store the results in a variable, and then check if the variable was not empty. For example, I use the following to compile all the .java files in a source directory.
如果您希望将文件列表作为批处理进行处理,而不是为每个文件执行单独的操作,则可以使用 find,将结果存储在变量中,然后检查变量是否为空。例如,我使用以下代码编译源目录中的所有 .java 文件。
SRC=`find src -name "*.java"`
if [ ! -z $SRC ]; then
javac -classpath $CLASSPATH -d obj $SRC
# stop if compilation fails
if [ $? != 0 ]; then exit; fi
fi
回答by dmaticzka
This one is suitable for use with the Unofficial Bash Strict Mode, no has non-zero exit status when no files are found.
这个适合与非官方 Bash 严格模式一起使用,当没有找到文件时,没有非零退出状态。
The array logfiles=(/var/log/apache2/access.log.*)
will always contain at least the unexpanded glob, so one can simply test for existence of the first element:
该数组logfiles=(/var/log/apache2/access.log.*)
将始终至少包含未扩展的 glob,因此可以简单地测试第一个元素是否存在:
logfiles=(/var/log/apache2/access.log.*)
if [[ -f ${logfiles[0]} ]]
then
echo 'At least one file found'
else
echo 'No file found'
fi
回答by skupeez
First, store files in the directory as an array:
首先,将目录中的文件存储为数组:
logfiles=(/var/log/apache2/access.log.*)
Then perform a test on the count of the array:
然后对数组的计数执行测试:
if [[ ${#logfiles[@]} -gt 0 ]]; then
echo 'At least one file found'
fi
回答by Marco Montel
Or using find
或者使用查找
if [ $(find /var/log/apache2/ -type f -name "access.log.*" | wc -l) -gt 0 ]; then
echo "ok"
else
echo "ko"
fi
回答by RobbySherwood
ls -1 /var/log/apache2/access.log.* | grep . && echo "One or more files exist."
回答by duparq
You just need to test if ls
has something to list:
您只需要测试是否ls
有要列出的内容:
ls /var/log/apache2/access.log.* >/dev/null 2>&1 && echo "exists one or more files"
回答by A. Richard
Variation on a theme:
主题变化:
if ls /var/log/apache2/access.log.* >/dev/null 2>&1
then
echo 'At least one file found'
else
echo 'No file found'
fi
回答by Kay Marquardt
more simplyfied:
更简单:
if ls /var/log/apache2/access.log.* 2>/dev/null 1>&2; then
echo "ok"
else
echo "ko"
fi