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.

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

2011-03-12 - Confoo 2011 presentation
Slides are available from the commandlinefu presentation at Confoo 2011: http://presentations.codeinthehole.com/confoo2011/
2011-01-04 - Moderation now required for new commands
To try and put and end to the spamming, new commands require moderation before they will appear on the site.
2010-12-27 - Apologies for not banning the trolls sooner
Have been away from the interwebs over Christmas. Will be more vigilant henceforth.
2010-09-24 - OAuth and pagination problems fixed
Apologies for the delay in getting Twitter's OAuth supported. Annoying pagination gremlin also fixed.
Hide

Tags

Hide

Functions

recursive search and replace old with new string, inside files

Terminal - recursive search and replace old with new string, inside files
$ grep -rl oldstring . |xargs sed -i -e 's/oldstring/newstring/'
2009-03-03 20:10:19
User: netfortius
Functions: grep sed
25
recursive search and replace old with new string, inside files

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'

Alternatives

There is 1 alternative - vote for the best!

Terminal - Alternatives
find . -type f -exec sed -i s/oldstring/newstring/g {} +
2009-12-09 00:46:13
User: SlimG
Functions: find sed
Tags: sed find
14

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

grep -rl oldstring . | parallel sed -i -e 's/oldstring/newstring/'
2010-01-28 08:44:16
Functions: grep sed
3

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

so sweet and delicious

Comment by linuxrawkstar 280 weeks and 6 days ago

egrep might be better in some cases

Comment by linuxrawkstar 280 weeks and 6 days ago

find . -type f -exec grep -l XXX {} \;|tee /tmp/fileschanged|xargs perl -pi.bak -e 's/XXX/YYY/g'

Find all files that contain string XXX in them, change the string from XXX to YYY, make a backup copy of the file and save a list of files changed in /tmp/fileschanged.

Comment by drossman 280 weeks and 4 days ago
find -type f | xargs sed -i -e '/oldstring/s,oldstring,newstring,'
Comment by rbossy 246 weeks and 4 days ago

grep before using sed is unnecessary, it causes all files to be read and processed TWICE instead of just once.

Just use find and sed.

Comment by SlimG 240 weeks and 6 days ago

If you can install "rpl", have a look at man rpl. It does the same job more simply. I don't know how it would compare for speed though.

Comment by johnraff 240 weeks and 6 days ago

if you do this on a directory using subversion, ignore the .svn directories, or you'll get a surprise when asking for svn status.

Comment by zaninottof 219 weeks and 1 day ago

this command has worked in all cases I've tried it on. I prefer to use fgrep, however. find also works nicely

Comment by bbrraacckk 54 weeks and 6 days ago

If you want to make sure you haven't already processed the line you can use this:

grep -rLZ newstring . |xargs -0 sed -i -e 's/oldstring/newstring/'

The -L tells grep to invert the search (-v doesn't do what you want) and the grep -Z and xargs -0 go together to ignore whitespace and allow spaces in filenames.

Comment by mrwulf 37 weeks and 4 days ago

Your point of view

You must be signed in to comment.

Related sites and podcasts