1

In Linux (command line):

I need to find all Perl-files (filename ends with .pl or .pm) that are located in /users/tom/ or any of its sub-directories and which contain both the string ->get( and the string #hyphenate (located in different lines or in the same line). I just need the names of the files (and their path's). I don't need the lines within the file where the strings was found.

Is there a command that can do this?

I know how to find files with one extension:

find /users/tom -name "*.pl"  

But i have troubles to find files, that have one of two different extensions. None of this commands works:

find /users/tom -name "*.pl" -name "*.pm"  
find /users/tom -name "*.pl|*.pm"  

My workaround is to do it one after the other, but I guess there must be a more elegant way.

Now for the file's content:
I know how to print filenames and matching lines with grep:

grep * -e "->get(" -e "#hyphenate"  

This lists all files that contain at least one of the search-strings. But I want a list of files that contain all search-strings.

How can this be done? (Form command-line in Ubuntu/Linux)

4
  • I don't get it, why can't find /usr/tom | egrep '*.pl| *.pm' work? Commented Nov 14, 2015 at 13:55
  • @Aftnix: Thanks, this is the solution for part 1 of my question. This gives me the list of all files in which I want to search for some strings. How do I perform this search? Commented Nov 14, 2015 at 14:01
  • Not a particularly good solution; a standard wildcard like find -name '*.p[lm]' would work better. Commented Nov 14, 2015 at 17:25
  • TL;DR: grep -ri 'my string' Commented Aug 6, 2020 at 3:39

3 Answers 3

4

grep can recursively search directories with -r. To only get file names, not the matching lines, use -l.

grep -rl -- '->get(\|#hyphenate' /users/tom | grep '\.p[lm]$'

Or, with find:

find /users/tom -name '*.p[lm]' -exec grep -l -- '->get(\|#hyphenate' {} +

Update

The above searches for ->get( or #hyphenate, if you want both, you have to run grep twice:

find /users/tom -name '*.p[lm]' -exec grep -l -- '->get(' {} + \
| xargs grep -l '#hyphenate'

If your file names contain whitespace, you might need to specify -Z for the first grep and -0 for xargs.

Sign up to request clarification or add additional context in comments.

3 Comments

This is not a proper solution; it will list files which contain any match, not all the search expressions.
@tripleee: You're right. I missunderstood the question.
@tripleee: should be fixed now.
2

grep -r PLACE_YOUR_STRING_HERE | cut -d ' ' -f 1 | grep '.p1\|.pm'

Replace the string with the pattern you are looking for and then run the command after going to the folder in which you wish to look.

Comments

0
find /usr/tom | egrep '*.pl| *.pm' | xargs cat | grep <PATTERN>

2 Comments

A lone * is strictly speaking a regex error, though most greps will simply skip the erroneous part of the expression. The reason ` *` works is accidental, as it will permit zero repetitions of the erroneous space.
Also, as ever, the lone cat is useless; you can add some flags to the final grep to avoid printing the names of matching files; but here, that appears to be the precise opposite of what the OP wants.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.