DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Snippets has posted 5883 posts at DZone. View Full User Profile

Shell Script For Find And Replace

  • submit to reddit
        Here's a little shell snippet for finding and replacing text in multiple files:

for fl in *.txt; do
mv $fl $fl.old
sed 's/find/replace/g' $fl.old > $fl


Snippets Manager replied on Sun, 2010/02/28 - 5:41pm

Just an FYI. Both the above posts seem tedious. You can do this in one line in a shell script or in your favorite CLI: (first cd /home/dir/of/choice **** make sure to check you have the correct directory with 'pwd' first!!!) Then: sed -i 's/search/replace/g' * SED is very powerful so please be careful. * means all files. you could also do *.py / *.pl / *.php -CHO AT FOUNDCO.COM

Snippets Manager replied on Mon, 2012/05/07 - 1:11pm

or, you could do: for i in `*.txt` do sed -i 's/find/replace/g' $i done