批量重命名Dropbox冲突文件

5
我有大量由dropbox服务错误生成的冲突文件,这些文件位于我的本地Linux文件系统中。
例如文件名为compile(master's conflicted copy 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
4个回答

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 命令(由于文件名中包含空格,所以在命令行中很难做到不使用额外脚本)。


感谢你的解决方案,Guntram。它让我找到了正确的方向。我已经采用了你提供的内容,并进行了修改/扩展以更好地适应我的需求。学到了更多关于Linux Bash的强大之处(敬意)。 - cemlo

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

1

一个小贡献:

我遇到了这个脚本的问题。文件名中带有空格的文件没有被复制。因此,我修改了第17行:

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

if test -f "$newname"

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


1

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接