This question arose from another question I had here ("How to extract basename of parent directory in shell"), which seems to have opened the "rabbit hole" down the Unix string manipulations for me. So, here goes supplementary question:
What is the correct way to extract various parts ("levels") from dirname results combined with find?
Let's assume I have the following hierarchy:
DE_AT/adventure/motovun/300x250/A2_300x250.zip I "find" the file like so:
find . -name "*.zip" execute shell on the findresults:
-exec sh -c '' {} \; How would I extract each part of the full path? How do I get:
- DE_AT
- adventure
- motovun
- 300x250
- A2_300x250.zip
This is what I know so far:
basename "$1" # gets me: A2_300x250.zip dirname "$1" # gets me: ./DE_AT/adventure/motovun/300x250 I am asking this because I need to rename this .zip files into someString_DE_AT_motovun+A2_300x250.zip.
I came up with a horrible frankensolution like so:
find . -name "*.zip" -exec sh -c ' mv "$0" "myString_$(basename $(dirname $(dirname \ $(dirname "$0")_...+$(basename "$0")" ' {} \; I don't even wish to try this because this simply cannot be correct.