Search code examples
linuxartifact

How to ignore a file using find command


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 ?


Solution

  • 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.