Bash rename extension recursive

后端 未结 6 1547
-上瘾入骨i
-上瘾入骨i 2020-12-29 09:32

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

This is what I got:

find . -name \"*.so\         


        
6条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-12-29 09:57

    This will do everything correctly:

    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) 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) It will handle spaces and any other special characters in filenames (except double quotes).

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

    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).

提交回复
热议问题