2015-01-14 53 views
0

我正在为网站编写一个makefile。从源目录复制html文件以建立目录

我有一个名为src/build/

基本上,我想借此文件这样的目录:

src/index.html 
src/blog/title1/index.html 
src/blog/title2/index.html 

,并将它们复制到build/目录是这样的:

build/index.html 
build/blog/title1/index.html 
build/blog/title2/index.html 

我试着写规则,但我不确定如何调试:

src_html := src/**/*.html 
build_html := $(shell find src -name '*.html' | sed 's/src/build/') 

$(src_html): $(build_html) 
    @cp $< [email protected] 
+0

首先,当通配符匹配时,您应该使用1 *而不是2。 –

回答

2

,如果你安装了它你可以使用rsync。

default: 
     rsync -r --include '*/' --include='*.html' --exclude='*' src/ build/ 
+0

该死的这是一个不错的解决方案,我想我必须做一个'rsync rsync'。为优雅的脚本+1! – ShellFish

1

尝试是这样的:

#! /bin/bash 

# get htm files 
find . -name '*html' > files 

# manipulate file location 
sed 's/src/build/' files | paste files - > mapping 

# handle spaces in the file names 
sed 's/ /\\ /' mapping > files 

# output mapping to be sure. 
cat files 
echo "Apply mapping?[Y/n]" 
read reply 
[[ $reply =~ [Yy].* ]] || exit 1 
# copy files from column one to column two 
awk '{ system("cp "$1" "$2)}' files 

exit 0 

编辑

无等待我有一个衬垫:

$ find -name '*html' -exec bash -c 'file=$(echo {}); file=$(echo $file | sed "s:\/:\\\/:g"); cp "{}" $(echo ${file/src/build} | sed "s:\\\/:\/:g")' \; 
+1

只需使用真正有用的_mmv_命令'mmv -vcd'src /; *。html''build /#1#2.html'' – bobbogo

+0

非常感谢你分享这个命令,我一定会仔细研究它! – ShellFish

1

只是为了保持完整性,使只能处理静态模式规则该罚款:

src := src/index.html src/blog/title1/index.html src/blog/title2/index.html 
# or src := $(shell find …) etc., but hopefully the makefile already has a list 

dst := $(patsubst src/%,build/%,${src}) 
${dst}: build/%: src/% ; cp $< [email protected] 

.PHONY: all 
all: ${dst} 

这是-j安全太,以及不复制尚未更新的文件。

相关问题