macos osx 更改文件编码 (iconv) 递归
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1182037/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
osx change file encoding (iconv) recursive
提问by ekkescorner
I know I can convert a single file encoding under OSX using:
我知道我可以使用以下方法在 OSX 下转换单个文件编码:
iconv -f ISO-8859-1 -t UTF-8 myfilename.xxx > myfilename-utf8.xxx
iconv -f ISO-8859-1 -t UTF-8 myfilename.xxx > myfilename-utf8.xxx
I have to convert a bunch of files with a specific extension, so I want to convert file encoding from ISO-8859-1 to UTF-8 for all *.ext files in folder /mydisk/myfolder
我必须转换一堆具有特定扩展名的文件,所以我想将文件夹 /mydisk/myfolder 中所有 *.ext 文件的文件编码从 ISO-8859-1 转换为 UTF-8
perhaps someobe know the syntax how to do this
也许有人知道如何做到这一点的语法
thanks
谢谢
ekke
埃克
回答by ekkescorner
Adam' comment showed me the way how to resolve it, but this was the only syntax I made it work:
Adam 的评论向我展示了如何解决它,但这是我让它工作的唯一语法:
find /mydisk/myfolder -name \*.xxx -type f | \
(while read file; do
iconv -f ISO-8859-1 -t UTF-8 "$file" > "${file%.xxx}-utf8.xxx";
done);
-i ... -o ... doesnt work, but >
-i ... -o ... 不起作用,但是 >
thx again
再次感谢
ekke
埃克
回答by ghostdog74
if your shell is bash, something like this
如果你的 shell 是 bash,像这样
for files in /mydisk/myfolder/*.xxx
do
iconv -f ISO-8859-1 -t UTF-8 "$files" "${files%.xxx}-utf8.xxx"
done
回答by Albert.Qing
Here is example Tested in mac 10.10. Find file by name,convert encode ,then replace original file.work perfect. Thanks for Roman Truba's example,COPY the full code below to your shell script.
这是在 mac 10.10 中测试的示例。按名称查找文件,转换编码,然后替换原始文件。完美运行。感谢 Roman Truba 的示例,将下面的完整代码复制到您的 shell 脚本中。
#!/bin/bash
find ./ -name *.java -type f | \
(while read file;
do if [[ "$file" != *.DS_Store* ]]; then
if [[ "$file" != *-utf8* ]]; then
iconv -f ISO-8859-1 -t UTF-8 "$file" > "$file-utf8";
rm $file;
echo mv "$file-utf8" "$file";
mv "$file-utf8" "$file";
fi
fi
done);
回答by Hardi
I extended Albert.Qings script:
我扩展了 Albert.Qings 脚本:
- autodetect the current file encoding
- added a command parameter to do a dry/exec-run
added a parameter for the directory and filename pattern
#!/bin/bash command=${1-"usage"} searchPattern=${2-"*.java"} searchDirectory=${3-"."} if [[ "$command" == "usage" ]]; then echo "convert-file-to-utf8.sh [usage|dry|exec] [searchPattern=$searchPattern] [searchDirectory=$searchDirectory]" exit fi find $searchDirectory -type f -name "$searchPattern" | \ (while read file; do if [[ "$file" != *.DS_Store* ]]; then if [[ "$file" != *-utf8* ]]; then currentEncoding="$(file --brief --mime-encoding $file)" if [[ "$currentEncoding" != "utf-8" ]]; then echo "command:$command / iconv -f $currentEncoding -t UTF-8 $file" if [[ "$command" == "exec" ]]; then iconv -f $currentEncoding -t UTF-8 "$file" > "$file-utf8"; rm $file; echo mv "$file-utf8" "$file"; mv "$file-utf8" "$file"; fi fi fi fi done);
- 自动检测当前文件编码
- 添加了一个命令参数来执行dry/exec-run
添加了目录和文件名模式的参数
#!/bin/bash command=${1-"usage"} searchPattern=${2-"*.java"} searchDirectory=${3-"."} if [[ "$command" == "usage" ]]; then echo "convert-file-to-utf8.sh [usage|dry|exec] [searchPattern=$searchPattern] [searchDirectory=$searchDirectory]" exit fi find $searchDirectory -type f -name "$searchPattern" | \ (while read file; do if [[ "$file" != *.DS_Store* ]]; then if [[ "$file" != *-utf8* ]]; then currentEncoding="$(file --brief --mime-encoding $file)" if [[ "$currentEncoding" != "utf-8" ]]; then echo "command:$command / iconv -f $currentEncoding -t UTF-8 $file" if [[ "$command" == "exec" ]]; then iconv -f $currentEncoding -t UTF-8 "$file" > "$file-utf8"; rm $file; echo mv "$file-utf8" "$file"; mv "$file-utf8" "$file"; fi fi fi fi done);
Tested on MacOS X 10.12.6 / Sierra.
在 MacOS X 10.12.6 / Sierra 上测试。
回答by Jakub Rulec
try this ... it′s tested and workin:
试试这个......它已经过测试并且可以正常工作:
First step (ICONV): find /var/www/ -name *.php -type f | (while read file; do iconv -f ISO-8859-2 -t UTF-8 "$file" > "${file%.php}.phpnew"; done)
第一步(ICONV):找到/var/www/ -name *.php -type f | (同时读取文件;做 iconv -f ISO-8859-2 -t UTF-8 "$file" > "${file%.php}.phpnew"; 完成)
Second step (REWRITE - MV):
find /var/www/ -name "*.phpnew" -type f | (while read file; do mv $file echo $file | sed 's/\(.*\.\)phpnew/\1php/'
; done)
第二步(REWRITE - MV):找到/var/www/ -name "*.phpnew" -type f | (同时读取文件;执行 mv $file echo $file | sed 's/\(.*\.\)phpnew/\1php/'
;完成)
It′s just conclusion on my research :)
这只是我研究的结论:)
Hope it helps Jakub Rulec
希望它可以帮助 Jakub Rulec
回答by Adam Rosenfield
If you want to do it recursively, you can use find(1)
:
如果要递归执行,可以使用find(1)
:
find /mydisk/myfolder -name \*.xxx -type f | \
(while read file; do
iconv -f ISO-8859-1 -t UTF-8 -i "$file" -o "${file%.xxx}-utf8.xxx
done)
Note that I've used | while read
instead of the -exec
option of find (or piping into xargs
) because of the manipulations we need to do with the filename, namely, chopping off the .xxx
extension (using ${file%.xxx}
) and adding -utf8.xxx
.
请注意,我用| while read
的,而不是-exec
找到的选项(或管道进入xargs
,因为我们需要做的文件名,即操纵),斩去.xxx
扩展(使用${file%.xxx}
)和加入-utf8.xxx
。
回答by Stefan Kendall
You could write a script in any scripting language to iterate over every file in /mydisk/myfolder, check the extension with the regex [.(.*)$], and if it's "ext", run the following (or equivalent) from a system call.
您可以使用任何脚本语言编写脚本来遍历 /mydisk/myfolder 中的每个文件,使用正则表达式 [.(.*)$] 检查扩展名,如果它是“ext”,请运行以下(或等效的)从一个系统调用。
"iconv -f ISO-8859-1 -t UTF-8" + file.getName() + ">" + file.getName() + "-utf8.xxx"
"iconv -f ISO-8859-1 -t UTF-8" + file.getName() + ">" + file.getName() + "-utf8.xxx"
This would only be a few lines in Python, but I leave it as an exercise to the reader to go through the specifics of looking up directory iteration and regular expressions.
这只是 Python 中的几行代码,但我将其作为练习留给读者,以了解查找目录迭代和正则表达式的具体细节。