2015-01-14 52 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安全太,以及不復制尚未更新的文件。

相關問題