Change file names with find and iconv

后端 未结 3 1091
天命终不由人
天命终不由人 2021-01-13 12:30

I\'ve tried to change filenames using following script:

find dir/ -type f -exec mv {} $(echo {} | iconv -f UTF8 -t ASCII//TRANSLIT ) \\;

Why doesn\'t it work

3条回答
  •  轻奢々
    轻奢々 (楼主)
    2021-01-13 13:13

    The problem with using $() in this way is that the subshell executes prior to executing the find command, and not as part of -exec. You can do it, but you'll need to invoke bash. Something like:

    find dir/ -type f -exec bash -c 'mv "$1" "$(iconv -f UTF8 -t ASCII//TRANSLIT <<< $1)"' -- {} \;
    

    Keep in mind this will also translit any special chars in directory names as well, which may cause the mv to fail. If you only want to translit the filename, then you could:

    find dir/ -type f -exec bash -c 'mv "$1" "${1%/*}/$(iconv -f UTF8 -t ASCII//TRANSLIT <<< ${1##*/})"' -- {} \;
    

    which split the directory portion off and only translits the file name.

提交回复
热议问题