【问题标题】:linux: find common files from two directories with single commandlinux:使用单个命令从两个目录中查找公共文件
【发布时间】:2020-07-25 02:36:26
【问题描述】:

目录1:[anyName]-test/target/surefire-reports/*.xml

目录2:target/surefire-reports/*.xml

jenkins shell cmd 我想出了:

sh "jar -cMvf  Test.zip target/surefire-reports/*.xml *-test/target/surefire-reports/*.xml "

只有一个目录存在(dir1 或 dir2),所以 shell 步骤总是因为没有文件或目录而失败。

有没有更好的办法在单个命令中查找 xml 文件而不会失败? (可能是一些正则表达式)谢谢!

【问题讨论】:

  • 将我的问题编辑得更清楚。

标签: linux directory find


【解决方案1】:

使用 GNU find:

find . -type f -regex '\./\([^/]*-test/\)?target/surefire-reports/[^/]*\.xml'\
  -exec jar -cMvf Test.zip {} +

-regex 操作与常规 (type -f) 文件的路径匹配。这只会从surefire-reports 目录中添加*.xml 文件,而不是从其子目录中添加。如果要包含子目录,请将[^/]*\.xml 替换为.*\.xml


使用 glob 模式的替代方案:

find target/surefire-reports *-test/target/surefire-reports -maxdepth 1 -type f\
  -name '*.xml' -exec jar -cMvf Test.zip {} +

如果要包含子目录,请删除 -maxdepth 1

target(您的项目目录)的父目录运行这两个命令。

【讨论】:

  • 非常感谢您的帮助。这两种解决方案都适用于 Dir1 和 Dir2 。对我来说,解决方案 1 就是我想要的 :)。由于替代选项在 shell 中总是会失败,没有这样的文件或目录,因为它同时查找两个目录,而我只存在一个目录。我会选择解决方案 1。非常感谢您的时间和努力。
【解决方案2】:

试试:

cd <TOP DIRECTORY>
find . -type f -name "*unitTest.xml" -print | xargs jar -cMvf Test.zip

我放了 "*unitTest.xml,因为在 Dir2 中 J 是大写的。 这样,它只会在 JUnitTtest.xml 文件存在时捕获它们。

所以find 命令的结果被用作jar 命令的参数。这是由xargs 完成的。 find 不关心文件是否存在,所以不会出错。

在 bash 上测试。

【讨论】:

  • 谢谢,它几乎没问题,但那些 xml 文件也存在于其他文件夹中,所以我不需要它们。我想从“(任何文件夹)-test/target/surefire-reports/*.xml ”或“target/surefire-reports/*.xml”
猜你喜欢
  • 2021-08-27
  • 1970-01-01
  • 2023-02-16
  • 1970-01-01
  • 1970-01-01
  • 2017-04-01
  • 2013-01-08
  • 2010-10-13
  • 1970-01-01
相关资源
最近更新 更多