0

I wrote a command that successfully returns all PDF files that contain the keyword "Font":

find /my/path/PDFFiles/  -type f -name "*.pdf" -exec grep -H "Font" '{}' ';'

There are some limitations with this command that I'd like to overcome.

Questions

  1. How would I use a RegEx for my search string where I am currently doing just "Font"
  2. How can I adjust the command above to return all filenames where the keyword "Font" does not exist?

Thanks

1 Answer 1

2

1) grep supports regular expressions out of the box. So just get rid of the "". For example, if you want "dont" or "font"

find /my/path/PDFFiles/  -type f -name "*.pdf" -exec grep -H "[df]ont" '{}' ';'

2) grep also allows you to exclude entries with a -v command. So to exclude 'Font' try:

find /my/path/PDFFiles/  -type f -name "*.pdf" -exec grep -vH "Font" '{}' ';'

You can also use finds prune option as detailed in this question

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

2 Comments

[df]ont, unquoted, will fail under zsh with the error zsh: no matches found: [df]ont (never executing the command). Quoted is always safer (but especially it wards off danger in the case that either ? or * are present in your expression, vulnerable to interpretation by the shell.)
Thank you @kampu! I've updated my answer accordingly. I've actually just switched to zsh but foolishly only checked it ran in bash! I'll know for next time. Good point on the "quoted is always safer".

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.