這裏是一個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