Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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?

share|improve this question
up vote 3 down vote accepted

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

jargrep someterm
share|improve this answer
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

Try something along the lines of

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

read the man page for find for more details.

share|improve this answer
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

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.