批量重命名保管箱冲突文件

塞姆洛

我有很多由保管箱服务生成(不正确)的冲突文件。这些文件在我的本地linux文件系统上。

示例文件名=编译(母版的冲突副本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
Guntram Blohm

当前目录中的所有文件:

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

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章