I want to replace a string with a different string in all directory and file names, recursively. So if I want to replace foo with bar and have this file:
foo_project/my_app/old_foo/start_foo.sh
I would want it to become
bar_project/my_app/old_bar/start_bar.sh
I have been able to do this, but it's pretty ugly. Writing it from memory (can't copy paste so forgive typos) the command I have is:
find $PROJECT_DIR -name "*foo*" | tac | xargs -n 1 -I % bash -c "eval mv % \$\( echo % \| sed \''s/\(.*\)foo/\1$PROJECT_NAME/'\' \)"
This works for me, but it's hideous. Is there a cleaner approach to do what I want?
To give context on above command, the issue I had was that early rename commands would break later mv commands by making the paths differ. That's why I switched to a tac before an xargs (instead of the -evalcmd argument for find I started with) to ensure that I ran the most deeply nested renames first. I then had to make my sed only rename the last instance of foo so I could move everything within a folder before renaming the folder.
The ugly part is the need of eval, and thus multiple levels of escaping arguments, because I didn't otherwise know how to pass the contents of xargs to the sed command. There has to be something cleaner then this though?