If you don't have tar with --transform , this might work:
TRG=/target/some/where SRC=/my/source/dir cd "$SRC" find . -type f -name \*.\* -printf "mkdir -p '$TRG/%h' && cp '%p' '$TRG/%p'\n" |\ sed 's:/\.::;s:/./:/:' |\ xargs -I% sh -c "%"
There are no spaces after \ , you need a simple end to the line, or you can append it to one line, for example:
find . -type f -name \*.\* -printf "mkdir -p '$TRG/%h' && cp '%p' '$TRG/%p'\n" | sed 's:/\.::;s:/./:/:' | xargs -I% sh -c "%"
Explanation:
find will find all simple files that have extensions in the SRC (source) directory- find
printf prepare the necessary shell commands:- to create the necessary directory tree in TRG (target directory)
- copy command
sed do a cleanup of the cosmetic path (e.g. fixing /some/path/./other/dir)xargs will take the whole line- and execute prepared commands using the shell
But it will be much better:
- just make an exact copy in the first step
- rename files in the second step
easier, cleaner and FASTER (no need to check / create target sub-racers)!
source share