Using bash, how can I search for all occurrences of the substring ‘foo’ in all filenames (including folders) contained recursively in a directory and replace them them with ‘bar’?
For example, if the current structure looks like:
-foo_test
- fooo.txt
- xfoo
- yfoo.h
- 1foo.c
It should look like this after running the bash script:
-bar_test
- baro.txt
- xbar
- ybar.h
- 1bar.c
Both variations shown here using work correctly on OPs test structure:
or, if you have a very large number of files and want it to run faster:
EDIT: As noted in the comments, my earlier answer using a
findcommand that did not use theexecdiroption and usingrenamehas problems renaming files in directories that contain foo in their name. As suggested, I have changed the find commands to use-execdir, and I have deleted the variation using therenamecommand since it is a non-standard command.