If you haven't tar
with --transform
this can works:
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 "%"
No spaces after the \
, need simple end of line, or you can join it to one line like:
find . -type f -name \*.\* -printf "mkdir -p '$TRG/%h' && cp '%p' '$TRG/%p'\n" | sed 's:/\.::;s:/./:/:' | xargs -I% sh -c "%"
Explanation:
- the
find
will find all plain files what have extensions in you SRC (source) directory
- the find's
printf
will prepare the needed shell commands:
- command for create the needed directory tree at the TRG (target dir)
- command for copying
- the
sed
doing some cosmetic path cleaning, (like correcting /some/path/./other/dir)
- the
xargs
will take the whole line
- and execute the prepared commands with shell
But, it will be much better:
- simply make an exact copy in 1st step
- rename files in 2nd step
easier, cleaner and FASTER (don't need checking/creating the target subdirs)!