我知道我可以使用轉換OSX下一個文件編碼:OSX更改文件編碼(的iconv)遞歸
iconv -f ISO-8859-1 -t UTF-8 myfilename.xxx > myfilename-utf8.xxx
我有一堆文件轉換成具有特定擴展名, 所以我想以文件的編碼從ISO-8859-1轉換爲UTF-8 在文件夾中所有* .EXT文件/ mydisk/MyFolder中
也許someobe知道語法如何做到這一點
感謝
EKKE
我知道我可以使用轉換OSX下一個文件編碼:OSX更改文件編碼(的iconv)遞歸
iconv -f ISO-8859-1 -t UTF-8 myfilename.xxx > myfilename-utf8.xxx
我有一堆文件轉換成具有特定擴展名, 所以我想以文件的編碼從ISO-8859-1轉換爲UTF-8 在文件夾中所有* .EXT文件/ mydisk/MyFolder中
也許someobe知道語法如何做到這一點
感謝
EKKE
亞當的評論給我怎樣的方式來解決它, 但是這是我做出的唯一語法它的工作:
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不工作,但>
THX再次
EKKE
你可以在任何腳本語言編寫一個腳本,在/ mydisk/MyFolder中的所有文件遍歷,檢查擴展與正則表達式[(。*)$],如果它的「內線」 ,從系統調用運行以下(或等效)。
「的iconv -f ISO-8859-1 -t UTF-8」 + file.getName()+ 「>」 + file.getName()+ 「-utf8.xxx」
這將只在Python中有幾行,但我把它作爲一個練習,讓讀者瞭解查找目錄迭代和正則表達式的細節。
,如果你的shell是bash,如果你想遞歸地做這樣的事情
for files in /mydisk/myfolder/*.xxx
do
iconv -f ISO-8859-1 -t UTF-8 "$files" "${files%.xxx}-utf8.xxx"
done
,您可以使用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)
請注意,我用| while read
代替-exec
由於我們需要對文件名進行操作,即刪除.xxx
擴展名(使用${file%.xxx}
),可以選擇查找(或者輸入xargs
) )並加入-utf8.xxx
。
查看ekkescorner的解答工作解決方案 – Kutzi 2010-11-12 15:08:48
試試這個...測試it's和幹活:
第一步(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)
第二步(REWRITE - MV): find/var/www/-name「* .phpnew」-type f | (同時讀取文件,做MV $文件echo $file | sed 's/\(.*\.\)phpnew/\1php/'
;完成)
It's剛剛結束我的研究:)
希望它可以幫助 的Jakub Rulec
我這裏是在Mac 10.10進行測試。 按名稱查找文件,轉換編碼,然後替換原來的file.work完美。 感謝羅馬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);
我擴展Albert.Qings腳本:
追加的參數爲目錄和文件名模式
#!/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);
在MacOS X 10.12.6/Sierra上測試。
覆蓋創建的文件#!/ bin/bash find ./tmp -type f | \ (while read file; do iconv -f windows-1251 -t UTF-8「$ file」-o「$ file」; done); ` – temni 2012-05-16 06:00:47
謝謝。我幫了我很多。我這麼做了`#!/ bin/bash 找到./src -type f | \ (同時讀取文件;做 \t如果[ 「$文件」= * .DS_Store *]!],然後 \t \t如果[ 「$文件」= * -utf8 *]!],然後 \t \t \t的iconv -f CP1251 -t UTF-8 「$文件」> 「$文件UTF8」; \t \t \t RM $文件; \t \t \t MV 「$文件UTF8」 「$文件」; \t \t音響 \t fi done);` – 2012-10-25 03:43:01