I have a lot of directories. Each one of these has a file with a specific extension .ext.
I want to rename these files.
Example:
// Note: The names are random, can have spaces and special characters
Parent
|
|- First Directory
| |
| |- Some file.txt
| |- Another one.pdf
| `- The one to rename.ext
|
|- Second Directory
| |
| |- Some file.txt
| |- Another one.pdf
| `- The one to rename.ext
[…]
I want to rename all the file with the .ext extension to new name.ext2. There is only one file with the .ext per directory, so no problem with that.
What I have done so far is:
find ./Parent -name "*.ext" -exec mv "{}" "{}.ext2" \;
And I get a bunch of The one to rename.ext.ext2, I am stuck about renaming them new name.ext2. If I just set it as the second argument of mv, it will move them into the Parent directory, and thus, each call to the mvfunction will overwrite the previous moved file.
Note: I couldn't make it work using the -exec bash -c '' argument. I tried something along the lines of:
find ./Parent -name "*.ext" -exec bash -c 'mv {} "$(dirname {})/new name.ext2"' \;
But I get some issue with the $() being expanded before {}
shopt -s globstar nullglobcommand. (c.f. comment on this answer) It doesn't work on OSX and I can't upgrade bash. – JoliG Apr 28 '16 at 06:02