I have a folder with txt, html, jpg and png files.
Now I want to go over all textbased files (txt and html) and start a search and replace.
I did the following and it is working fine:

find ./ -iregex '.*\.\(txt\|html\)$' -exec sed -i 's/search/replace/g' {} +

But now I also want to log the files in which a replacement was executed.
Let's say I have five txt and/or html files in my folder. But in only two of them there will be something replaces because the string search is in them.
So after the command I want to have a log file (for example myLog.log) in which the two files are printed (full path).

But how can I achieve such a command?
The sed command does not have such a parameter/option.


You will have to search fist for search pattern, then sed, and log.

try (line break for readability)

find ./ -iregex '.*\.\(txt\|html\)$' \
    -exec grep -q search {} \;  \
    -print \
    -exec sed -i 's/search/replace/g' {} + \
    > my-sed.log


  • search in -exec grep -q search {} \; is your search pattern. Note that you can't use + who will match more that one file.
  • -print print matched file, and thus can log
  • 1
    I would also probably change that messy -iregex thing into \( -iname '*.txt' -o -iname '*.html' \) for clarity (and also to make it somewhat more portable). – Kusalananda Feb 20 at 13:29

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.