I have a find command that works perfectly except that it doesn't sort the results. path = path to the folder where you want to search foo = search term

find /path/ -name '*.pdf' -exec sh -c 'pdftotext "{}" - | grep --with-filename --label="{}" --color -i "foo"' \;

result (on my screen 'foo' is in red)

me@myComp ~ $ find /path/ -name '*.pdf' -exec sh -c 'pdftotext "{}" - | grep --with-filename --label="{}" --color -i "foo"' \;
/path/lesson 05.pdf:a foo
/path/lesson 05.pdf:  (to have) a foo when I was 10 years old.
/path/lesson 07.pdf:a foo
/path/lesson 07.pdf:Elephant – foo – heavy
/path/lesson 07.pdf:Elephant – foo – heavy – light
/path/lesson 07.pdf:tigers – high – foos – to jump
/path/lesson 04.pdf:10 My foo (not to eat) fat.
/path/lesson 06.pdf:A: John lost the foos collar. B: Is this its ? (rarely used)
/path/lesson 06.pdf:A: This is my foo. .......... is a chihuahua. .......... name is Sleeper.

Is there some way to sort the result alphabetically on the path / filename while keeping foo red? As you can see lesson 04.pdf is between lesson 07.pdf and lesson 06.pdf.

using | sort at the end gives the desired results except that the foo isn't in red anymore.

thank you very much

  • 1
    You can pass the output from sort to another grep to highlight 'foo'. See unix.stackexchange.com/a/367/173368 .
    – Haxiel
    Mar 7, 2020 at 6:29
  • thank you that works for me. The only drawback is that I have to type in the search term two times.
    – anatak
    Mar 13, 2020 at 6:38

1 Answer 1


I think this should do what you want:

while read file; do pdftotext "$file" | grep --with-filename --label="$file" --color -i "foo"'; done < <(find /path/ -name '*.pdf' | sort)

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .