Home > other >  How to ignore a file using find command
How to ignore a file using find command

Time:04-10

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 ?

CodePudding user response:

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.

CodePudding user response:

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.

CodePudding user response:

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

  • Related