击重命名扩展递归 [英] Bash rename extension recursive

查看:193
本文介绍了击重命名扩展递归的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道有很多这样的东西左右,但他们要么不递归工作或者他们是巨大的。

I know there are a lot of things like this around, but either they don't work recursively or they are huge.

这是我得到了什么:

find . -name "*.so" -exec mv {} `echo {} | sed s/.so/.dylib/` \;

当我刚跑了找兼职它给我的文件列表。当我运行SED部分它用名为.dylib任何的.so。当我一起运行他们不工作。

When I just run the find part it gives me a list of files. When I run the sed part it replaces any .so with .dylib. When I run them together they don't work.

我换成呼应MV,看看发生了什么:

I replaced mv with echo to see what happened:

./AI/Interfaces/C/0.1/libAIInterface.so ./AI/Interfaces/C/0.1/libAIInterface.so

没有被替换了!结果
什么是错的?

Nothing is replaced at all!
What is wrong?

推荐答案

这会尽一切的正确


find -L . -type f -name "*.so" -print0 | while IFS= read -r -d '' FNAME; do
    mv -- "$FNAME" "${FNAME%.so}.dylib"
done

通过正确,我们的意思是:

By correctly, we mean:

1),将重命名的只是文件扩展名的(由于使用 $ {FNAME%。所以}的名为.dylib )。所有使用其他解决方案 $ {X /的.so /名为.dylib} 是不正确的,因为他们错误地命名的第一次出现的的.so 中的文件名(如 x.so.so 重命名为 x.dylib.so ,或者更糟, ./库/ libTemp.so-1.9.3 / libTemp.so 重命名为 ./库/ libTemp名为.dylib-1.9.3 / libTemp.so - 一个错误)

1) It will rename just the file extension (due to use of ${FNAME%.so}.dylib). All the other solutions using ${X/.so/.dylib} are incorrect as they wrongly rename the first occurrence of .so in the filename (e.g. x.so.so is renamed to x.dylib.so, or worse, ./libraries/libTemp.so-1.9.3/libTemp.so is renamed to ./libraries/libTemp.dylib-1.9.3/libTemp.so - an error).

2)将处理空间和文件名中的任何其它特殊字符(除双引号)。

2) It will handle spaces and any other special characters in filenames (except double quotes).

3)它不会改变目录或其他特殊文件。

3) It will not change directories or other special files.

4)将遵循符号链接到子目录和链接到目标文件并重新命名目标文件,而不是链接本身(FIND的默认行为是处理符号链接本身,而不是文件指向的链接)

4) It will follow symbolic links into subdirectories and links to target files and rename the target file, not the link itself (the default behaviour of find is to process the symbolic link itself, not the file pointed to by the link).

这篇关于击重命名扩展递归的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆