I need to rename a couple of files using shell scripting, by a certain "key". This key includes both strings as well as extracted portions of file path that I get with find.
I am on a Mac, OSX El Capitan and am using ZSH. Here is the directory tree:
├── 300x250
│ ├── 300x250-img-fallback.jpg
│ └── index/
├── 300x600
│ ├── 300x600-img-fallback.jpg
│ └── index/
├── 336x280
│ ├── 336x280-img-fallback.jpg
│ └── index/
└── 970x250
├── 970x250-img-fallback.jpg
└── index/
I need to rename ../index/ folders into ../c2_[parentFolderName]/. This is what I am trying:
find . -type d -mindepth 2 -maxdepth 2 -exec sh -c 'echo -- mv "$0" "$(dirname "$0")"/"C2_"$(basename "$0/..")""' {} \;
This doesn't seem to be the proper way to get the basename of the parent unfortunately.
find . -name "*index" -exec sh -c 'echo -- mv "$0" "$(dirname "$0")"/"C2_"$(basename "$0/..")""' {} \;
This one is just a variation which also does not work (there is no reason why it should :) ).
I am quite new to shell scripting and am trying to learn as much as possible in a shell agnostic kind of way, so please disregard that I am using ZSH currently.