Hide

What's this?

commandlinefu.com is the place to record those command-line gems that you return to again and again.

Delete that bloated snippets file you've been using and share your personal repository with the world. 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.


If you have a new feature suggestion or find a bug, please get in touch via http://commandlinefu.uservoice.com/

Get involved!

You can sign-in using OpenID credentials, or register a traditional username and password.

First-time OpenID users will be automatically assigned a username which can be changed after signing in.

Universal configuration monitoring and system of record for IT.
Hide

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:

Hide

News

May 19, 2015 - A Look At The New Commandlinefu
I've put together a short writeup on what kind of newness you can expect from the next iteration of clfu. Check it out here.
March 2, 2015 - New Management
I'm Jon, I'll be maintaining and improving clfu. Thanks to David for building such a great resource!
Hide

Top Tags

Hide

Functions

Hide

Credits

recursive search and replace old with new string, inside files

Terminal - recursive search and replace old with new string, inside files
find . -type f -exec sed -i s/oldstring/newstring/g {} +
2009-12-09 00:46:13
User: SlimG
Functions: find sed
15
recursive search and replace old with new string, inside files

This command find all files in the current dir and subdirs, and replace all occurances of "oldstring" in every file with "newstring".

Alternatives

There are 4 alternatives - vote for the best!

Terminal - Alternatives
$ grep -rl oldstring . |xargs sed -i -e 's/oldstring/newstring/'
2009-03-03 20:10:19
User: netfortius
Functions: grep sed
Tags: perl sed
25

recursively traverse the directory structure from . down, look for string "oldstring" in all files, and replace it with "newstring", wherever found

also:

grep -rl oldstring . |xargs perl -pi~ -e 's/oldstring/newstring'
grep -rl oldstring . | parallel sed -i -e 's/oldstring/newstring/'
2010-01-28 08:44:16
Functions: grep sed
4

xargs deals badly with special characters (such as space, ' and "). To see the problem try this:

touch important_file

touch 'not important_file'

ls not* | xargs rm

Parallel https://savannah.nongnu.org/projects/parallel/ does not have this problem.

grep -rlZ oldstring . | xargs -0 sed -i -e 's/oldstring/newstring/'
2011-10-18 19:14:02
Functions: grep sed xargs
Tags: sed
1

Using -Z with grep and -0 with xargs handles file names with spaces and special characters.

replace old new -- `find -type f`
2012-12-13 20:22:17
User: brian
Tags: sed find
1

Search and replace recursively. :-) Shorter and simpler than the others. And allows more terms:

replace old new [old new ...] -- `find -type f`

find . -name "*.txt" | xargs -n 1 perl -pi -w -e "s/text([0-9])/other\$1/g;"
2014-02-28 06:38:38
User: kennethjor
Functions: find perl xargs
0

Does a search and replace across multiple files with a subgroup replacement.

Know a better way?

If you can do better, submit your command here.

What others think

Using sed you can use the 'g' option in case there is more than one occurance of the "oldstring" in the matching sentence.

Also find has the '+' sign to behave like using xargs.

find . -type f -exec sed -i "s/oldstring/newstring/g" {} +;
Comment by JesusSuperstar 298 weeks and 2 days ago

Without the "/g" sed will replace the first occurrence of oldstring in each line, not the first in each file.

Comment by johnraff 298 weeks and 1 day ago

Thanks for the tip, using + is much faster, guess it's because + doesn't start sed more than once, I've changed the command accordingly.

Using "g" with sed is already explained in the comment of the command

Comment by SlimG 298 weeks and 1 day ago

Thanks for the knowlege johnraff, didn't see your comment before I posted, changed the command and description accordingly.

Comment by SlimG 298 weeks and 1 day ago

Don't you need a semicolon (;) at the end?

Comment by eev2 192 weeks and 3 days ago

Your point of view

You must be signed in to comment.