skip to Main Content

I would like to search for the term "Liszt" within the contents of all text files except their file name. So if there was a file called Liszt Mazeppa.txt it should not be matched as the term "Liszt" is in the file name.

Conversely if there were a file called "Masur LGO.txt" whose contents were Beethoven 3; Liszt Orpheus this should be matched.

I would not use find as from how I understand it find is used for searching for file names.

I tried:
grep -TiPr --include="*.txt" "Liszt" > ./ matches.txt

Unfortunately this still matches text files which have the word Liszt in them.

2

Answers


  1. Assuming you want the filenames of the files containing the word Liszt

    find . -type f ! -name '*Liszt*' -exec grep -lw Liszt {} ;
    

    edit

    Your rewritten question does not add much.
    If you really don’t care about the filenames you can remove the -name condition from the previous find.

    Consider what you want when a file whose name contains Liszt also has Liszt in its content.

    Login or Signup to reply.
  2. Regarding I would not use find as from how I understand it find is used for searching for file names. – your use of GNU grep‘s messy options to both find files and search within files is confusing you.

    Don’t use grep to find files, there’s a perfectly good tool to find files with a very obvious name, find. The GNU guys really messed up when they gave GNU grep -r and all those other options for finding files thereby giving people the possibility of making grep commands unnecessarily convoluted.

    Finding files and searching within files are 2 conceptually separate things so use find to find files and then grep to g/re/p (Globally match a Regular Expression and Print the result) within files.

    find . -type f -name '*.txt' -exec grep -HTi 'Liszt' {} +
    

    I’m just guessing at the grep options you’ll want from reading your existing code since you didn’t provide any expected output.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search