cem*_*mlo 5 linux rename dropbox
我有大量由 Dropbox 服务(错误地)生成的冲突文件。这些文件位于我的本地 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
Run Code Online (Sandbox Code Playgroud)
当前目录中的所有文件:
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
Run Code Online (Sandbox Code Playgroud)
或者要递归,请将以下内容放入我将调用的脚本中/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
Run Code Online (Sandbox Code Playgroud)
然后find . -type f -print0 | xargs -0 -n 1 /tmp/myrename(如果不使用额外的脚本,在命令行上执行此操作有点困难,因为文件名包含空格)。
| 归档时间: |
|
| 查看次数: |
1049 次 |
| 最近记录: |