Every time this situation pops up, which is about once every few months, I end up spending way too long trying to figure out a bash script that does exactly what I want. I am pretty sure what I want is similar to what everyone else wants, but more often than not the available scripts online when I do a quick search are oversimplified and don’t do these things (and my own bash scripting skills aren’t good enough to figure it out):
- It needs to be recursive (traverse an entire folder tree)
- It must allow me to choose the type of file (e.g. “*.js”) because sometimes the folder tree contains binaries and other file types that shouldn’t be searched
- It should replace the text in-place, editing the existing files
- It shouldn’t cause source control (git, svn) to think the file has been modified unless it actually has been modified (I don’t know why but sed seems to do this)
- It should allow for regular expressions
What is the SIMPLEST way to accomplish this? I mean, I could write a script in python, it would only be a few lines, but I feel like this should be easy to accomplish in the shell.
I typically do something like this:
I then see if I’m getting the files of interest. If I am, I add to the pipeline:
And let ‘er rip.
If sed is making your VCS think files are changed when they aren’t, you should look at the diffs. Perhaps you have whitespace turned off in your diffs but that’s what’s changing (line endings or the like)?
I’m sure someone will come and point out that one or both of my uses of xargs up there are not necessary, but for one-off jobs, it doesn’t matter. I prefer these multi-program pipelines for this sort of task anyway, since I can easily disable or swap out certain filters and check the results of the rest if the results are not correct at first.