如何使用 find 命令忽略文件

发布于 2025-01-20 01:52:20 字数 300 浏览 5 评论 0原文

我正在尝试使用命令

  • 名称找到伪影:获取通往Java Artifact的路径

    运行:echo java_artifact = $(findbuild/libs/*。jar -type f)> $ gt; $ github_env

  1. Echo Java_artifact = $(findbuild /
  2. ) libs/abc-plain.jar

我只想选择abc.jar文件。

谁能建议我如何实现这一目标?

I'm trying to find artifact using the command

  • name: Get path to Java artifact

    run:echo JAVA_ARTIFACT=$(findbuild/libs/*.jar -type f) >>$GITHUB_ENV

The problem is I have 2 artifacts in that directory

  1. build/libs/abc.jar
  2. build/libs/abc-plain.jar

I want to pick only abc.jar file.

Can anyone suggest how can I achieve this ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

浸婚纱 2025-01-27 01:52:20

find 命令可以与正则表达式一起使用,这样可以轻松获得任何类型的复杂搜索结果。工作原理:

  1. 您必须将 find 命令与 -regex 一起使用,而不是 -name
  2. 您必须生成匹配的正则表达式

find 如何将文件名传递给正则表达式?

假设我们有以下目录结构:

/home/someone/build/libs/abc.jar
/home/someone/build/libs/abc-plain.jar

并且我们坐在 someone 中,

如果我们在没有任何进一步参数的情况下执行 find .,我们会得到:

./build/libs/abc.jar
./build/libs/abc-plain.jar

因此,我们可以使用正则表达式搜索:

  1. 以单点 .
  2. 开头的文件名中可能有一些附加路径
  3. 的任意数量的 - 字符
  4. ,不应包含以 .jar< 结尾 /code>

这会导致:

  1. “.”
  2. '/*'
  3. '[^-]+'
  4. '.jar'

一起:

find . -regex '.*/[^-]+.jar'

或者如果您只想在 build/libs/ 中搜索

find ./build/libs -regex '. */[^-]+.jar'

您可以在那里找到在线正则表达式工具

The find command can be used with regular expressions which makes it easy to get any kind of complex search results. How it works:

  1. You have to use your find command with -regex instead of -name.
  2. You have to generate a matching regular expression

How find passes the filename to the regular expression?

Assume we have the following directory structure:

/home/someone/build/libs/abc.jar
/home/someone/build/libs/abc-plain.jar

and we are sitting in someone

if we execute find . without any further arguments, we get:

./build/libs/abc.jar
./build/libs/abc-plain.jar

So we can for example search with regex for:

  1. something starts with a single dot .
  2. may have some additional path inside the file name
  3. should NOT contain the - character in any number of character
  4. ends with .jar

This results in:

  1. '.'
  2. '/*'
  3. '[^-]+'
  4. '.jar'

And all together:

find . -regex '.*/[^-]+.jar'

or if you ONLY want to search in build/libs/

find ./build/libs -regex '.*/[^-]+.jar'

You find a online regex tool there.

提赋 2025-01-27 01:52:20

查找命令支持标准UNIX等级匹配,包括或排除文件。您可以轻松地使用Regex编写复杂的查询,同时查找命令将每个/file/to/to/path的目录树递归下降,以评估表达式。

The find command support standard UNIX regex to match, include or exclude files. You can write complex queries easily with regex while finding the command recursively descends the directory tree for each /file/to/path listed, evaluating an expression.

夏の忆 2025-01-27 01:52:20

由于您尚未清楚地提到您不希望文件名中的连字符-,因此我假设在没有> -的情况下找到文件。

我会尝试这样的事情。匹配下案例,上案例,数值&amp; .jar带有REGEX的扩展。

查找build/libs/-regextype posix-egrep -regex'。*/[a-za-z0-9]+\。jar'

我在本地测试时的输出低于输出。

touch abc.jar
touch abc-plain.jar
find . -regextype posix-egrep -regex '.*/[a-zA-Z0-9]+\.jar'
./abc.jar

您可以尝试以上命令在这里

Since you haven't clearly mentioned that you don't want the hyphen - in the filename, I'm assuming to find files without -.

I would try something like this. Matching lower-case, upper-case, numerical & .jar extension with regex.

find build/libs/ -regextype posix-egrep -regex '.*/[a-zA-Z0-9]+\.jar'

I got below output when tested locally.

touch abc.jar
touch abc-plain.jar
find . -regextype posix-egrep -regex '.*/[a-zA-Z0-9]+\.jar'
./abc.jar

You can try above commands here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文