2011-07-09 16 views
0

需要大時間幫助。我完全停留在這一個。我循環(遞歸)通過目錄中的所有文件並打印文件列表。例如:如何在unix中使用shell腳本替換for ... in循環中的部分路徑到文件?

# srcDir="input received from command line" 
# tgtDir="input received from command line" 

for listItem in `find ${srcDir}` 
do 
    if [[ -f ${listItem} ]] 
    then 
     echo ${listItem} # **** what to put here **** 
    fi 
done 

打印列表時,我想將文件的路徑從srcDir替換爲tgtDir。例如:

# let's assume that srcDir="/home/user" 
# and tgtDir="/tmp/guest" 
# srcDir has the following structure 
file1.txt 
dir1_1/file1a.txt 
dir1_1/file1b.txt 

# so the actual output of the script will be 
/home/user/file1.txt 
/home/user/dir1_1/file1a.txt 
/home/user/dir1_1/file1b.txt 

# but I want it to print as 
/tmp/guest/file1.txt 
/tmp/guest/dir1_1/file1a.txt 
/tmp/guest/dir1_1/file1b.txt 

我希望你明白了。請指教。

回答

1

看起來像bash string operations工作:

srcDir="/home/user" 
tgtDir="/tmp/guest" 
listItem="/home/user/file1.txt" 
echo ${listItem/$srcDir/$tgtDir} 
+0

BANG ON!謝謝噸薩姆...你是男人! – Mandeep

相關問題