1

I want to write a script (or preferably an alias) that does the following:

  1. Find all the files matching a pattern *.jar
  2. Iterate over all those files, and run jar tvf <jar file name> | grep <Some argument>
  3. Print if a jar contains a file with a name that matches the argument

I'm working on tcsh and would prefer not to open a new shell since I want this as an alias and not a script - is it doable?

RonK
  • 241
  • 1
  • 5
  • 13

2 Answers2

3

A straight alias isn't going to work for this application. Something like this would come close, except what you would get is the output of the grep, not the jar filename:

alias targrep="find . -name '*.jar' -exec jar tvf {} \; | grep"

targrep someterm

To get the filename, you should switch to using a function instead of an alias so that you have more control over arguments and where values go. (Note: bash format, you may need to modify for tcsh)

function jargrep () {
    find . -name '*.jar' | while read jar; do
        jar tvf $jar | grep -q  $@ && echo $jar
    done
}

jargrep someterm
Caleb
  • 11,813
  • 4
  • 36
  • 49
  • Not really working. This works: `find . -name '*.jar' -exec jar tvf '{}' \;` But this: `find . -name '*.jar' -exec jar tvf '{}' | grep AAA \;` does not – RonK May 12 '11 at 11:46
  • @RonK: Appologies, my answer was bunk from start to finish. I just re-wrote it taking into account the real problem, which is getting the filename of the jar instead of just the match of the grep (I assume this is what you were after but my earlier solution, even with corrected syntax, would have failed to give that output). – Caleb May 12 '11 at 19:27
-1

Try something along the lines of

find . -name '*.jar' -exec 'jar tvf | grep <some agrument>'

read the man page for find for more details.

wolfgangsz
  • 8,847
  • 3
  • 30
  • 34
  • 1
    You need to include the `{}` place marker for where to put the tar file name and also terminate the `-exec` argument with `\;` or `+`. Also this will not work as an alias like he wanted because the arguments to grep are embedded in the string, not the last thing on line. – Caleb May 12 '11 at 11:25