9

I am trying to find all the jars which has spring in its name. I am working with windows and using cygwin to run the linux commands. Does my below command looks right?

find . -name "*.jar" -exec jar tf '{}' \| grep -H "spring" \;

For some reason, it is still processing and haven't got the result on the screen. Any fast way to grep the jar file with a particular name?

For example, below are the valid jar files which has spring in it -

spring-web.jar
spring-core.jar
test-spring.jar 
6
  • You can try without grep like this. find /some/dir -type f -name "*spring*.jar"
    – Ramesh
    Jun 16, 2014 at 20:17
  • @Ramesh, OP's looking to check the jar listing for spring
    – iruvar
    Jun 16, 2014 at 20:19
  • @1_CR, yeah, so my command is also to search all jar files which has the word spring in it.
    – Ramesh
    Jun 16, 2014 at 20:21
  • Your command is looking for a file name with spring in it, but it is looking at the files in the jar. Jun 16, 2014 at 21:26
  • @richar: no only the jar name which has spring in its name.
    – arsenal
    Jun 16, 2014 at 23:36

2 Answers 2

4

I do not believe the jar command accepts multiple .jar files by way of arguments so AFAIK a single jar invocation addressing all .jar files is out of the question. One option to speed things up by avoiding one find exec invocation for each .jar file is the follows

find . -name '*.jar' -exec sh -c \
'for f; do  jar -tf "$f" | grep --label="$f" "spring"; done' sh {} +
0
2

The below command would look for the jar files that have substring spring in it.

find /some/dir -type f -name "*spring*.jar"

Now, your original command looks through the content, which isn't what the text around it or the examples say.

find . -name "*.jar" -exec jar tf '{}' \| grep -H "spring" \;

Please find this chat description where Gilles explains why the above command is not what you are looking for.

Testing

[root@keithpc check]# cd jar_check/
[root@keithpc jar_check]# ls
[root@keithpc jar_check]# touch spring1.jar
[root@keithpc jar_check]# touch 1spring.jar
[root@keithpc jar_check]# touch no_sss_spring.jar
[root@keithpc jar_check]# touch spring.txt

Output:

[root@keithpc jar_check]# find ./ -type f -name "*spring*.jar"
./no_sss_spring.jar
./1spring.jar
./spring1.jar

As you can see, the spring.txt file is not returned which I believe is what you are looking for.

Now, to understand what your command does,

  1. First I have created a java file which has the name as springtest.
  2. Now, I create the class file by running the java command on the java file.
  3. Now, I add it to the jar file by running the jar command.
  4. Now, when I run your command, it will return me the class file rather than the jar file that you are looking for.

     [root@keithpc jar_check1]# javac springtest.java
     [root@keithpc jar_check1]# java springtest
     Hello,spring
     [root@keithpc jar_check1]# jar cvf springtest.jar springtest.class
     added manifest
     adding: springtest.class(in = 426) (out= 286)(deflated 32%)
    
     ##Here I run your command after creating the jar file. 
    
     [root@keithpc jar_check1]# find . -name "*.jar" -exec jar tf '{}' \| 
     grep -H "spring" \;
     ##As you can see, it returns me the class file rather than the 
     jar file as the output.  
    
     springtest.class
    

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .