I use the following line to replace a string in files:

ag -Q -l "$1" | xargs sed -i '' "s/$1/$2/g"

ag is the_silver_searcher, of course.

How can I also count the number of files that were affected by this change?

1 Answer 1


You could use tee in the pipe line and create a temporary file, then run wc -l to count the number of files (lines).

ag -Q -l "$1" | tee -a /tmp/num | xargs sed -i '' "s/$1/$2/g"; wc -l /tmp/num; rm /tmp/num
  • nice, but I would really avoid creating temp files
    – ɹoƃı
    Sep 13, 2017 at 5:54

You must log in to answer this question.

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