commandlinefu.com is the place to record those command-line gems that you return to again and again.
You can sign-in using OpenID credentials, or register a traditional username and password.
Subscribe to the feed for:
recursively traverse the directory structure from . down, look for string "oldstring" in all files, and replace it with "newstring", wherever found
grep -rl oldstring . |xargs perl -pi~ -e 's/oldstring/newstring'
There are 6 alternatives - vote for the best!
This command find all files in the current dir and subdirs, and replace all occurances of "oldstring" in every file with "newstring".
xargs deals badly with special characters (such as space, ' and "). To see the problem try this:
touch 'not important_file'
ls not* | xargs rm
Parallel https://savannah.nongnu.org/projects/parallel/ does not have this problem.
Using -Z with grep and -0 with xargs handles file names with spaces and special characters.
Search and replace recursively. :-) Shorter and simpler than the others. And allows more terms:
replace old new [old new ...] -- `find -type f`
Does a search and replace across multiple files with a subgroup replacement.
If you can do better, submit your command here.
You must be signed in to comment.