与发现和iconv更改文件名 [英] Change file names with find and iconv

查看:170
本文介绍了与发现和iconv更改文件名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图更改使用下面的脚本文件名:

找到DIR /型的F -exec MV {} $(回声{} |的iconv -f -t UTF8 ASCII // TRANSLIT)\\;

为什么它不工作?我的意思是,当我有像'A',应该将其转换为A字符的文件。

  $回声ążźćó| -f的iconv -t UTF8 ASCII // TRANSLIT
azzco

为什么它不工作,找到-exec?

  $找到DIR /型的F -exec MV {} $(回声{} |的iconv -f -t UTF8 ASCII // TRANSLIT)\\;
MV:`DIR /齐亚'和'DIR /齐亚'是同一个文件

我得到使用xargs的相同的结果:

  $找到DIR /型的F | xargs的-I {}回声{} | -f的iconv -t UTF8 ASCII // TRANSLIT
DIR /齐亚

  $找到DIR /型的F | xargs的-I {} MV {} $(回声{} |的iconv -f -t UTF8 ASCII // TRANSLIT)
MV:`DIR /齐亚'和'DIR /齐亚'是同一个文件


解决方案

使用问题 $()这种方式是子shell执行时的在执行找到命令,而不是作为 -exec 。你可以这样做,但你需要调用的bash。是这样的:

 找到DIR /型的F -exec的bash -c'MV$ 1$(的iconv -f -t UTF8 ASCII // TRANSLIT<<< $ 1) ' -  {} \\;

请这也将在TRANSLIT目录名的任何特殊字符为好,这可能会导致 MV 失败。如果你只是想TRANSLIT文件名,然后你可以:

 找到DIR /型的F -exec的bash -c'MV$ 1$ {1%/ *} / $(-f的iconv -t UTF8 ASCII // TRANSLIT&LT ;&所述;&下; $ {1 ## * /})' -  {} \\;

其中分割的目录部分关闭,仅translits文件名

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? I mean when I have a file with character like 'ą' it should convert it to 'a'.

$ echo ążźćó | iconv -f UTF8 -t ASCII//TRANSLIT
azzco

why does it not work in find -exec?

$ find dir/ -type f -exec mv {} $(echo {} | iconv -f UTF8 -t ASCII//TRANSLIT ) \;
mv: `dir/zią' and `dir/zią' are the same file

I get the same results using xargs:

$ find dir/ -type f | xargs -I{} echo {} | iconv -f UTF8 -t ASCII//TRANSLIT
dir/zia

but:

$ find dir/ -type f | xargs -I{} mv {} $(echo {} | iconv -f UTF8 -t ASCII//TRANSLIT)
mv: `dir/zią' and `dir/zią' are the same file

解决方案

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.

这篇关于与发现和iconv更改文件名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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