I’m sure there are many different ways to do this however this way worked well. I’ve included an example where escaping specific characters is required which adds a little more complexity as they must be escaped using the ‘\’ character. You can also remove the ‘l’ in the grep command to see the resulting files as well when performing checks.

grep -rl '\.\.\.data' | xargs sed -i 's/\.\.\.data/\.\.\/data/g'

What is going on under the hood:

grep -rl: search recursively, and print the files that contain specifically “…data”
xargs: take the output of the grep command and passes as input to the next sed command
sed -i ‘s/\.\.\.data/\.\.\/data/g’: search and replace, within each file, old path with new path

Find & Replace Multiple Files
Tagged on:         

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.