1

I have a private project called A I now want to call B. I can easily move the content from A to B, but how do I rename all content A to be now B? Specifically

const A = 1;

I want to replace that with

const B = 1;

In a smart way, where I replace all occurrences of A with B recursively?

davidkonrad
  • 83,997
  • 17
  • 205
  • 265
  • The best solution is probably to use a language-specific refactoring tool. What language and editor or IDE are you using? – ChrisGPT was on strike Apr 16 '19 at 00:27
  • I am not using any IDE. Using gedit... Just thinking about deep replace of strings in multiple filles – davidkonrad Apr 16 '19 at 00:32
  • Okay, what language? Replacing strings is error prone: consider `const A = 1;` vs. the string `'A'` or a string _containing_ `A` like `'A potato'`. (I know you're not literally wanting to replace `A`, but the challenge remains.) A language-aware refactoring tool that understands some level of semantics is going to do a better job. [It doesn't look like Gedit supports any](https://wiki.gnome.org/Apps/Gedit/RequestedPlugins), but there may be standalone tools that you can use. Or you could briefly use a more powerful editor. – ChrisGPT was on strike Apr 16 '19 at 00:35
  • Well, it is a mix of js, php, md, sql and so on. Just need a hint to recursively replace inside files. – davidkonrad Apr 16 '19 at 00:57

1 Answers1

2

If you are sure about your expression to replace, you can try one of the solutions of "How to do a recursive find/replace of a string with awk or sed?"

Typically: a findfollowed with a -exec sed -i.

That would be enough to replace a fixed string by another.

Then add, commit and push back to your GitHub repo.

The OP davidkonrad confirms in the comments:

As a case sensitive search/replace within the directory:

sudo find ./ -type f -exec sed -i -e 's/A/B/g' {} \; 
Community
  • 1
  • 1
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • Thank you, that headed me in the right direction. I have no clou about the `+` but after some attempts it actually worked out with `sudo find ./ -type f -exec sed -i -e 's/A/B/g' {} \;` === **case sensitive** search/replace within the directory. There should be a GUI tool simplifying this to us newbies with only +15 yrs experience with Linux :) – davidkonrad Apr 17 '19 at 13:25
  • 1
    @davidkonrad I agree. The "+" was short for "followed by". I will edit the answer to make that clearer, and to include your command. – VonC Apr 17 '19 at 13:32