bash - How do I copy directory structure containing placeholders -


i have situation, template directory - containing files , links (!) - needs copied recursively destination directory, preserving attributes. template directory contains number of placeholders (__notation__), need renamed values.

for example template looks this:

./template/__placeholder__/name/__placeholder__/prog/prefix___filename___blah.txt 

destination becomes this:

./destination/project1/name/project1/prog/prefix_customer_blah.txt 

what tried far this:

# first create dest directory structure while read line;   dest="$(echo "$line" | sed -e 's#__placeholder__#project1#g' -e 's#__filename__#customer#g' -e 's#template#destination#')"   if ! [ -d "$dest" ];     mkdir -p "$dest"   fi done < <(find ./template -type d)  # copy files while read line;   dest="$(echo "$line" | sed -e 's#__placeholder__#project1#g' -e 's#__filename__#customer#g' -e 's#template#destination#')"   cp -a "$line" "$dest" done < <(find ./template -type f) 

however, realized if want take care permissions , links, going endless , complicated. there better way replace __placeholder__ "value", maybe using cp, find or rsync?

i suspect script want, if replace

find ./template -type f 

with

find ./template ! -type d 

otherwise, obvious solution use cp -a make "archive" copy of template, complete links, permissions, etc, , then rename placeholders in copy.

cp -a ./template ./destination while read path;   dir=`dirname "$path"`   file=`basename "$path"`   mv -v "$path" "$dir/${file//__placeholder__/project1}" done < <(`find ./destination -depth -name '*__placeholder__*'`) 

note you'll want use -depth or else renaming files inside renamed directories break.

if it's important directory tree created names changed (i.e. must never see placeholders in destination), i'd recommend using intermediate location.


Comments