I want to search for a certain string in several files using absolute path, say that I want to find all occurrences of inheritDoc in sevral java files, I have tried the following command:

grep -inr \
--include /opt/li/projects/universign-copy-2/universign/universign-server/src/com/cryptolog/universign/server/HealthCheckHandler.java \
--include /opt/li/projects/universign-copy-2/universign/universign-admin-www/src/com/cryptolog/universign/admin/actions/HealthCheckAction.java \
--include /opt/li/projects/universign-copy-2/universign/universign-www/src/com/cryptolog/universign/www/action/HealthCheckAction.java \
--include /opt/li/projects/universign-copy-2/universign/universign-core/src/com/cryptolog/universign/core/HealthCheckConfiguration.java \
--include /opt/li/projects/universign-copy-2/universign/universign-core/src/com/cryptolog/universign/core/HealthCheckResult.java \
--include /opt/li/projects/universign-copy-2/universign/universign-core/src/com/cryptolog/universign/core/utils/HealthCheckUtils.java \
--include /opt/li/projects/universign-copy-2/universign/universign-core/src/com/cryptolog/universign/core/utils/HttpUtils.java \

But that does not work. How could I possibly do this?

  • Have you tried including them in a file and then using -f? You can even pipe in a list with -f - and the << operator. Try man grep for information on grep switches. – tu-Reinstate Monica-dor duh Jul 26 at 8:50
  • @tudor If I include them in a file, I will not have information like this (for each file) : HealthCheckHandler.java:32:public class HealthCheckHandler implements StreamHandler, this result is from the normal grep -inr comand, I need the filename information – Bohao LI Jul 26 at 8:55
  • 2
    @Bohao LI : See -H option switch grep -iHn 'needle' /path1/file1 /path2/file2 <...> /pathN/fileN OR grep -iHn 'needle' $(cat /list/of/files.txt)-- Note that --include may be used with wildcards files selection – cmak.fr Jul 26 at 9:01
  • @cmak.fr Thanks, it works well. I looked at the "man grep" and finally realized that if we don't provide the "-r" option, "-H" is the default behavior. So "grep -in" == "grep -inH". – Bohao LI Jul 26 at 9:35

While you've give a pattern of files to '--include' (which will override any of these previously excluded by an '--exclude' argument) you haven't actually listed the files you want to search. Try:

grep -inr inheritDoc \
/opt/li/projects/universign-copy-2/universign/universign-server/src/com/cryptolog/universign/server/HealthCheckHandler.java \
/opt/li/projects/universign-copy-2/universign/universign-admin-www/src/com/cryptolog/universign/admin/actions/HealthCheckAction.java \
/opt/li/projects/universign-copy-2/universign/universign-www/src/com/cryptolog/universign/www/action/HealthCheckAction.java \
/opt/li/projects/universign-copy-2/universign/universign-core/src/com/cryptolog/universign/core/HealthCheckConfiguration.java \
/opt/li/projects/universign-copy-2/universign/universign-core/src/com/cryptolog/universign/core/HealthCheckResult.java \
/opt/li/projects/universign-copy-2/universign/universign-core/src/com/cryptolog/universign/core/utils/HealthCheckUtils.java \

And check 'man grep' for more information.

  • Options "-r" does not work, "-r" means recursively search in sub-directories, "-H" worked. Read the comments above. – Bohao LI Jul 26 at 11:33

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.