0

I'm trying to do a find and replace function, finding files which match a criteria then find/replace text within them.

Find statement (works find and returns list of files):

find / -type f -name "*.properties" -o -name "*.xml" -not \( -path '/tmp/*' -o -path '/var/tmp/*' \)

Sed find/replace:

sed -i 's/find/replace/g' {} \;

Putting together:

find / -type f -name "*.properties" -o -name "*.xml" -not \( -path '/tmp/*' -o -path '/var/tmp/*'  \) -exec sed -i 's/10\.32\.19\.156/10.32.19.165/g' {} \;

However this does not seem to work. Removing some 'find' parameters causes it to work, for example this works:

find / -type f -name "*.properties" -exec sed -i 's/10\.32\.19\.156/10.32.19.165/g' {} \;

How can I get sed to work with the extended 'find' parameters? Currently these two 'find' statements return exactly the same result in a test folder with only 2 files:

find /var/tmp/ipreplace/ -type f -name "*.properties"

find /var/tmp/ipreplace/ -type f -name "*.properties" -o -name "*.xml" -not \( -path '/tmp/*' -o -path '/var/tmp/*'  \)
Finn Andersen
  • 359
  • 2
  • 13
  • Not sure why you would want to use -not in your find query? find /var/tmp/ipreplace will restrict find to search in that directory only and other parameters are not necessary. – Deano Apr 11 '16 at 05:41

1 Answers1

0

I guess the use of -path parameter in your find command is wrong. Try the following:

find / -not \( -path '/tmp' -prune \) -not \( -path '/var/tmp' -prune \) -type f -name "*.properties" -o -name "*.xml" -exec sed -i 's/10\.32\.19\.156/10.32.19.165/g' {} \;

Look at this post for reference

Community
  • 1
  • 1
oliv
  • 12,690
  • 25
  • 45