Nov 25
To replace a string in all files inside a folder you can create a bash file:
1 | vi replace.sh |
Put inside this file:
1 2 3 4 5 6 | #!/bin/bash for file in $(ls -1); do sed 's/me/you/g' $file > /tmp/dummy.txt; mv /tmp/dummy.txt $file; done |
Save it and make it executeable (chmod 777 replace.sh)
In this case the string “me” will be replace with “you” in all files of the actual folder and the files will be overridden.
Another example
Your want to replace the tag <b> with <strong> in all files inside a special folder. Further you want to backup the original files with suffix .old and the new files will be overridden.
Simply type on the console:
1 2 3 4 5 | for file in $(ls -1); do mv $file $file.old sed 's/<b>/<strong>/g' $file.old > $file; done |
You can modify the script(s). Maybe by adding parameters etc.
Another option is to use sed with -i command or direct editing:
1 2 3 4 | for file in $(ls -1); do sed -i 's/text_to_replace/text_replacement/g' $file done |
