2013-12-21 162 views
3

我有大量的由保管箱服務生成(不正確)的衝突文件。這些文件在我的本地linux文件系統上。批量重命名Dropbox衝突文件

示例文件名=編譯(碩士矛盾複製2013年12月21日).SH

我想重新命名其正確的原始名稱的文件,在這種情況下compile.sh並刪除任何現有文件,那個名字。理想情況下,這可以是腳本或以這種方式遞歸。

編輯

望過去提供的解決方案和玩弄,並進一步研究後,我拼湊起來的東西,對我來說效果很好:

#!/bin/bash 

folder=/path/to/dropbox 

clear 

echo "This script will climb through the $folder tree and repair conflict files" 
echo "Press a key to continue..." 
read -n 1 
echo "------------------------------" 

find $folder -type f -print0 | while read -d $'\0' file; do 
    newname=$(echo "$file" | sed 's/ (.*conflicted copy.*)//') 
    if [ "$file" != "$newname" ]; then 
     echo "Found conflict file - $file" 

     if test -f $newname 
     then 
      backupname=$newname.backup 
      echo " " 
      echo "File with original name already exists, backup as $backupname" 
      mv "$newname" "$backupname" 
     fi 

     echo "moving $file to $newname" 
     mv "$file" "$newname" 

     echo 
    fi 
done 

回答

2

所有文件從當前目錄:

for file in * 
do 
    newname=$(echo "$file" | sed 's/ (.*)//') 
    if [ "$file" != "$newname" ]; then 
     echo moving "$file" to "$newname" 
#  mv "$file" "$newname"  #<--- remove the comment once you are sure your script does the right thing 
    fi 
done 

或進行遞歸,將以下內容放入一個腳本中,我將撥打/tmp/myrename

file="$1" 
newname=$(echo "$file" | sed 's/ (.*)//') 
if [ "$file" != "$newname" ]; then 
    echo moving "$file" to "$newname" 
#  mv "$file" "$newname"  #<--- remove the comment once you are sure your script does the right thing 
fi 

然後find . -type f -print0 | xargs -0 -n 1 /tmp/myrename(由於文件名包含空格,因此在命令行上沒有使用額外的腳本,這有點困難)。

+0

感謝您的解決方案Guntram。它使我走上正軌。我已採取您提供的內容並對其進行了修改/擴展,以更好地適應我的目的。瞭解更多關於linux bash(尊重)的力量。 – cemlo

1

小貢獻:

我這個腳本有問題。名稱中包含空格的文件不會被複制。所以我修改了17行:

-------切-------------切---------

if test -f "$newname"

------- cut ------------- cut ---------

1

上面顯示的這個腳本現在已過時;細跟在寫作時對Linux Mint的運行Dropbox的最新版本以下工作:

#!/bin/bash 

#modify this as needed 
folder="./" 
clear 

echo "This script will climb through the $folder tree and repair conflict files" 
echo "Press a key to continue..." 
read -n 1 
echo "------------------------------" 

find "$folder" -type f -print0 | while read -d $'\0' file; do 
    newname=$(echo "$file" | sed 's/ (.*Case Conflict.*)//') 
    if [ "$file" != "$newname" ]; then 
     echo "Found conflict file - $file" 

     if test -f "$newname" 
     then 
      backupname=$newname.backup 
      echo " " 
      echo "File with original name already exists, backup as $backupname" 
      mv "$newname" "$backupname" 
     fi 

     echo "moving $file to $newname" 
     mv "$file" "$newname" 

     echo 
fi 
done