0

I have the command

find . -type f \( ! -name "*.png" \) -print0 | \
xargs -0 sed -i 's#word#replace#g'

awk/sed: How to do a recursive find/replace of a string?
BASH: recursive program to replace text in a tree of files

This command works so far but i want to show the files in which sed replaces text. Is there some parameter which allows that?

Community
  • 1
  • 1
fechnert
  • 1,215
  • 2
  • 12
  • 30

3 Answers3

2

You can use the print and exec options together and print out the files that it processes:

find . -type f \( ! -name "*.png" \) -print -exec sed -i 's#word#replace#g' {} \; 2>/dev/null

0

You are missing cmp to compare two files pre and post change. Something on these lines you could try:

find . -type f \( ! -name "*.png" \) -exec sh -c 'sed "s/word/replace/g" $0 > $0.$$; if ! cmp $0.$$ $0; then echo $0; mv $0.$$ $0; else rm $0.$$; fi' {} \;
  • find - in current directory all the files name not like *.png
  • exec - sed search and replace word by relace word in file found by find command and put it in temp file appended by process id
  • if !cmp statement - compare both the files new and old and if they are not same then print file name along with the output and at the end move temp file to orginal file if they are not same else delete the temp file.
SMA
  • 36,381
  • 8
  • 49
  • 73
0

I don't know what platform you are on, but this is a possibility. Change your xargs to run a shell so you can do multiple commands and then inside that shell, test if the file is newer than some arbitrary file you created at the start - i.e. it is changed

touch /tmp/go
find ... | xargs ... -I % sh -c 'sed "%"; [ "%" -nt /tmp/go ] && echo %'
Mark Setchell
  • 191,897
  • 31
  • 273
  • 432