0

非常需要帮助。我完全坚持这个。我正在(递归地)遍历目录中的所有文件并打印文件列表。就像是:

# 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

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

4

1 回答 1

1

看起来像是bash 字符串操作的工作:

srcDir="/home/user"
tgtDir="/tmp/guest"
listItem="/home/user/file1.txt"
echo ${listItem/$srcDir/$tgtDir}
于 2011-07-09T23:15:13.110 回答