Trying to search a file directory for file names beginning with "h" and containing "enu"?

So far this is what I've got, I just don't know how to handle both grep statements in one command line?

find fs282/path/web/en_US.dic -exec grep "^h" {} \;
  • @ilkkachu sorry for confusion. Files with names starting with h and containing enu – ElonCode Dec 12 '18 at 13:49
  • What does the fs282/path/web/en_US.dic file (?) have to do with this, or is it a directory? – Kusalananda Dec 12 '18 at 13:52
  • @ElonCode, yep, it's the use of grep that I found confusing, as grep is mainly used to look at the contents of files. – ilkkachu Dec 12 '18 at 13:53

To look at the file names, use the -name condition of find. It should take regular filename patterns, so h*enu* matches what you want.

find /some/path -name "h*enu*"

grep would instead look at the contents of the files, line by line, so this would look for lines matching the pattern:

find /some/path -type f -exec grep '^h.*enu' /dev/null {} +

(The output would be the lines, not just the file names.)

grep expects a regular expression as opposed to glob wildcards for -name. Also, grep looks for matches within the lines as opposed to full matches with -name, hence the ^ to anchor the pattern at the start of the line. The /dev/null is only there to make sure the file names are always printed for each matching line, even when there's only one matching file. With the GNU implementation of grep, one may use the -H option instead.

  • (argh, brainfarted with that grep, thanks Stéphane.) – ilkkachu Dec 12 '18 at 14:51

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.