I'll try to explain my problem as best as I can, but please pardon my English ..

What I'm trying to do, seemed simple enough to me, however every solution I've found so far just won't work.

I wanna create a shell script that "reviews" every file in a directory based on a partial name, then check if each file found contains a specific string, and if so, return 1 or 0.

The code I tried so far: enter image description here

dont mind the #note

  • find /home/it21704/labs_team/ -type f -name "*somefilename*" -exec grep -c 'somestring' {} + Will return the count of found string in files. Also don't post screenshots, rather paste code you were trying to achieven the goal. – Valentin Bajrami Dec 11 '18 at 13:14
  • so you would like to grep a partial string in all files...am i right? – msp9011 Dec 11 '18 at 13:23
  • that is correct yes @msp9011 – otimalakiatheleis Dec 11 '18 at 13:47
  • '$name' will output literal $name. Better use "$name" in that case. – pLumo Dec 11 '18 at 14:53

This should output files which names match *$name* and including pattern:

shopt -s globstar # needed to be able to use **
grep -l "pattern" **/*"$name"*

You can disable globstar afterwards again if you wish:

shopt -u globstar

Alternative using find:

find . -name "*$name*" -exec grep -l "pattern" {} \;

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.