gpt4 book ai didi

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

转载 作者:可可西里 更新时间:2023-11-01 11:49:21 25 4
gpt4 key购买 nike

我有大量由保管箱服务(错误地)生成的冲突文件。这些文件在我的本地 Linux 文件系统上。

例子文件名=compile(master的冲突副本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

最佳答案

当前目录下的所有文件:

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

然后 找到 . -类型 f -print0 | xargs -0 -n 1/tmp/myrename(如果不使用额外的脚本,这在命令行上执行起来有点困难,因为文件名包含空格)。

关于linux - 批量重命名保管箱冲突文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20723868/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com