Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

Using a shell like bash or zshell, how can I do a recursive 'find and replace'? In other words, I want to replace every occurrence of '/path/to/folder' with '/other/dir' in all files in this directory and its subdirectories.

This is essentially the same question as the original from this name but what I need to know is what happens if the text you want to replace has slashes in it like the above examples? Can I just paste my code in the find/replace spot into the args? Or do I have to do something else???

All of this will be done over ssh to Ubuntu server.

Thanks!

share|improve this question
1  
your question is relly not clear maybe it will help if you add the code you like to use - if you have trouble with the slashes escaping them may help –  konqui Jun 26 at 4:10
    
The thing is that I know nothing about RegEx. Is there a place to learn it? –  Elliot Labs Jun 26 at 11:08
    
yes there is a lot of stuff about regex in the internet but take care about the fact that regex is not equal to regex (it depends a lot on the programming/script language your using/you'd like to use) - do you just want to do this a visual way (just show it on terminal/console) or do you wan't it to also change the directory in real (move files/rename folders)??? –  konqui Jun 26 at 11:15
    
I need to do it ALL in the console. It is on Godaddy servers through SSH –  Elliot Labs Jun 26 at 11:20
    
ok but what would you like to do just get a output to save in a text file or do an actual task e.g moving/renaming multiple files? –  konqui Jun 26 at 20:23

1 Answer 1

up vote 1 down vote accepted

Use find and sed:

find . exec sed -i .bak 's,/path/to/folder,/other/dir,g' \;
  • You can also use awk or perl instead of sed - pick whatever you like.
  • You can pass options to find to limit the file you operate on &c
share|improve this answer
    
Thanks! I know how to do the find part it was the wording of sed that was the issue. I am going to test it now :) –  Elliot Labs Jun 26 at 11:09
1  
Thanks! I used it like this:find html -name ".htaccess" -print0 | xargs -0 sed -i `` -e 's,/dir/with/a/lot/of/subs,/other/dir/with/some/more/subs,g' –  Elliot Labs Jun 26 at 11:24
    
It worked like a charm and that just took 200+ file edits of my back :) –  Elliot Labs Jun 26 at 11:24
    
Thanks for the proper syntax! –  Elliot Labs Jun 26 at 11:24

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.