使用 shell 变量查找具有多个文件名的名称 [英] find -name with multiple filenames using shell variable
问题描述
我有一个 find
命令可以查找名称与 -name
参数中提到的多个模式匹配的文件
I have a find
command that finds files with name matching multiple patterns mentioned against the -name
parameter
find -L . ( -name "SystemOut*.log" -o -name "*.out" -o -name "*.log" -o -name "javacore*.*" )
这会在命令行成功找到所需的文件.我正在寻找的是在 shell 脚本中使用此命令并将其与 tar
命令连接以创建所有日志文件的 tar.因此,在一个脚本中,我执行以下操作:
This finds required files successfully at the command line. What I am looking for is to use this command in a shell script and join this with a tar
command to create a tar of all log files. So, in a script I do the following:
LIST="-name "SystemOut*.log" -o -name "*.out" -o -name "*.log" -o -name "javacore*.*" "
find -L . ( ${LIST} )
这不会打印我正在寻找的文件.
This does not print files that I am looking for.
首先 - 为什么这个脚本不像命令那样运行?完成后,我可以使用 cpio
或类似方法一次性创建 tar
吗?
First - why this script is not functioning like the command? Once it does, can I club it with cpio
or similar to create a tar
in one shot?
推荐答案
看起来 find
无法匹配来自未引用变量的模式中的 *
.这种语法对我有用(使用 bash 数组):
Looks like find
fails to match *
in patterns from unquoted variables. This syntax works for me (using bash arrays):
LIST=( -name *.tar.gz )
find . "${LIST[@]}"
您的示例将变为以下内容:
Your example would become the following:
LIST=( -name SystemOut*.log -o -name *.out -o -name *.log -o -name javacore*.* )
find -L . ( "${LIST[@]}" )
这篇关于使用 shell 变量查找具有多个文件名的名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!