-1

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 ?

Rob
  • 14,746
  • 28
  • 47
  • 65
  • 1
    You got many valid answers. Why you don't accept them? Why you don't give any votes? You should respect how SO works :-) – Klaus Apr 09 '22 at 20:37

3 Answers3

2

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.

Klaus
  • 24,205
  • 7
  • 58
  • 113
  • Simple yet elegant. Mine follows a bit of a different approach wherein they have to exclusively specify the kind of special characters they need to filter. Yours exclude unwanted special characters. – harshavmb Apr 09 '22 at 08:13
  • @harshavmb Both solutions will fit as long we don't know more about the real requirement. If file names contain other chars or we should exclude some more complex strings post `-` or whatever... that is the power of regex :-) But yes, simply excluding all for containing `-` excepts also file names with special characters... but for standard use cases, both solutions works as expected. – Klaus Apr 09 '22 at 08:16
1

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.

0

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

harshavmb
  • 3,404
  • 3
  • 21
  • 55