Display a list of all java files which name is *.java in your current working directory which contain the string Example, regardless of the case sensitivity of the latter's individuals letters.

Example file containing any instances of example, EXAMPLE, etc.

  • 4
    U&L is not a place for people to post their homework assignments and ask others to do their work for them... You should show us what you tried, at least... Not to mention that similar questions must have been answered here already... – John WH Smith May 25 '15 at 17:53

grep -i example *.java

Displays a list of files with the lines that match.

  • could use -R option too to search recursively, what do you think? – tachomi May 25 '15 at 17:58
  • 3
    @tachomi Adding -R would search recursively in directories with names matching *.java. This assumes that they are directories with such silly names, and that they contain nothing but Java files (because every thing in those directories would be grep-ed). – John WH Smith May 25 '15 at 18:01
  • 1
    Yeah, you can't use -R together with a glob pattern that you want matched on each file. If you want to do that recursively, use find. – goldilocks May 25 '15 at 18:05


find . ! -name . -prune -type f -name '*.java' -exec grep -i example /dev/null {} +
  • Actually, I think -not is POSIX-compliant, while ! isn't. Some BSD find(1) require !, while GNU find supports both. – lcd047 May 25 '15 at 18:27
  • @lcd047: Can you give me the reference? pubs.opengroup.org/onlinepubs/9699919799/utilities/find.html said that ! was defined by POSIX while -not wasn't. – cuonglm May 25 '15 at 18:32
  • Never mind. There are contradictory claims in a few man pages, but IEEE Std 1003.1-2008 actually requires !. Sorry for the noise. – lcd047 May 25 '15 at 18:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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