a find and replace within text-based files, to locate and rewrite text en mass.

find . -name "*.txt" | xargs perl -pi -e 's/old/new/g'
syntax follows regular command line expression. example: let's say you have a directory (with subdirs) that has say 4000 .php files. All of these files were made via script, but uh-oh, there was a typo! if the typo is "let's go jome!" but you meant it to say "let's go home!" find . -name "*.php" | xargs perl -pi -e "s/let\'s\ go\ jome\!/let\'s\ go\ home\!/g" all better :) multiline: find . -name "*.php" | xargs perl -p0777i -e 's/knownline1\nknownline2/replaced/m' indescriminate line replace: find ./ -name '*.php' | xargs perl -pi -e 's/\".*$\"/\new\ line\ content/g'
Sample Output
command will have no echo

6
By: neztach
2009-02-06 00:28:03

What Others Think

obviously this command can get incredibly complicated depending on what you are replacing. bear in mind if you run this at root, the script will be indiscriminate!!
neztach · 658 weeks and 4 days ago
This is what sed was made for. Look at its -i option.
retardo · 658 weeks and 4 days ago
Here's the sed equivalent: find . -name '*.txt' -exec sed -ir 's/old/new/g' {} \;
porges · 658 weeks and 3 days ago
How did you? Im in awe life insurance
aegyjingoo · 42 weeks ago

What do you think?

Any thoughts on this command? Does it work on your machine? Can you do the same thing with only 14 characters?

You must be signed in to comment.

What's this?

commandlinefu.com is the place to record those command-line gems that you return to again and again. That way others can gain from your CLI wisdom and you from theirs too. All commands can be commented on, discussed and voted up or down.

Share Your Commands



Stay in the loop…

Follow the Tweets.

Every new command is wrapped in a tweet and posted to Twitter. Following the stream is a great way of staying abreast of the latest commands. For the more discerning, there are Twitter accounts for commands that get a minimum of 3 and 10 votes - that way only the great commands get tweeted.

» http://twitter.com/commandlinefu
» http://twitter.com/commandlinefu3
» http://twitter.com/commandlinefu10

Subscribe to the feeds.

Use your favourite RSS aggregator to stay in touch with the latest commands. There are feeds mirroring the 3 Twitter streams as well as for virtually every other subset (users, tags, functions,…):

Subscribe to the feed for: