我们来谈谈通配符如何工作一分钟.
cp *.txt foo
如果存在与该glob匹配的任何文件,则实际上不会使用参数* .txt调用cp.相反,它运行如下:
cp a.txt b.txt c.txt foo
同样,像
mv *.txt *.old
…不可能知道该怎么做,因为当它被调用时,它看到的是:
mv a.txt b.txt c.txt *.old
或者更糟糕的是,如果您已经有一个名为z.old的文件,它会看到:
mv a.txt b.txt c.txt z.old
因此,您需要使用不同的工具.考虑:
# REPLACES: mv /data/*/Sample_*/logs/*_Data_time.err /data/*/Sample_*/logs/*_Data_time_orig.err
for f in /data/*/Sample_*/logs/*_Data_time.err; do
mv "$f" "${f%_Data_time.err}_Data_time_orig.err"
done
# REPLACES: cp /data/*/Sample_*/scripts/*.sh /data/*/Sample_*/scripts/*_orig.sh
for f in /data/*/Sample_*/scripts/*.sh; do
cp "$f" "${f%.sh}_orig.sh"
done
# REPLACES: sh /data/*/Sample_*/scripts/*_orig.sh
for f in /data/*/Sample_*/scripts/*_orig.sh; do
if [[ -e "$f" ]]; then
# honor the script's shebang and let it choose an interpreter to use
"$f"
else
# script is not executable, assume POSIX sh (not bash, ksh, etc)
sh "$f"
fi
done
在添加新名称之前,它使用parameter expansion去除旧名称的尾部.