2013-12-21 78 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