这里是一个bash脚本:
source="/some/dir"
dest="/another/dir"
find "$source" -maxdepth 1 -type f -printf "%f\n" | while read -r file
do
suffix=
if [[ -a "$dest/$file" ]]
then
suffix=".new"
fi
# to make active, comment out the next line and uncomment the line below it
echo 'mv' "\"$source/$file\"" "\"$dest/$file$suffix\""
# mv "source/$file" "$dest/$file$suffix"
done
后缀是盲目添加的。如果在两个目录中都有名为“foo.new”的文件,那么结果将是一个名为“foo.new”的文件,而第二个名为“foo.new.new”的文件可能看起来很傻,但是正确的是它没有覆盖文件。但是,如果目的地已经包含“foo.new.new”(并且“foo.new”位于源和目的地中),则“foo.new.new”将被覆盖)。
您可以将上面的if
更改为循环以处理这种情况。这个版本也保留扩展:
source="/some/dir"
dest="/another/dir"
find "$source" -maxdepth 1 -type f -printf "%f\n" | while read -r file
do
suffix=
count=
ext=
base="${file%.*}"
if [[ $file =~ \. ]]
then
ext=".${file##*.}"
fi
while [[ -a "$dest/$base$suffix$count$ext" ]]
do
((count+=1))
suffix="."
done
# to make active, comment out the next line and uncomment the line below it
echo 'mv' "\"$source/$file\"" "\"$dest/$file$suffix$count$ext\""
# mv "$source/$file" "$dest/$file$suffix$count$ext"
done
欢迎来到SO! :) – 2010-03-30 21:24:25
@Katie - 我冒昧地编辑你的问题,并把你在评论中阐明的两件事情都放进去。 – DVK 2010-03-30 23:03:56