【发布时间】:2016-05-07 06:12:34
【问题描述】:
我有这样的情况,模板目录 - 包含文件和链接 (!) - 需要递归复制到目标目录,保留所有属性。模板目录包含任意数量的占位符 (__NOTATION__),需要将其重命名为特定值。
例如模板如下所示:
./template/__PLACEHOLDER__/name/__PLACEHOLDER__/prog/prefix___FILENAME___blah.txt
目的地变成这样:
./destination/project1/name/project1/prog/prefix_customer_blah.txt
到目前为止我尝试的是这样的:
# first create dest directory structure
while read line; do
dest="$(echo "$line" | sed -e 's#__PLACEHOLDER__#project1#g' -e 's#__FILENAME__#customer#g' -e 's#template#destination#')"
if ! [ -d "$dest" ]; then
mkdir -p "$dest"
fi
done < <(find ./template -type d)
# now copy files
while read line; do
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)
但是,我意识到,如果我想处理权限和链接,这将是无穷无尽的并且非常复杂。是否有更好的方法将 __PLACEHOLDER__ 替换为“值”,例如使用 cp、find 或 rsync?
【问题讨论】:
标签: bash shell scripting rsync placeholder