LoginSignup
2
1

More than 5 years have passed since last update.

ファイルの文字コードを推定するコマンド(Mac)

Posted at

概要

ファイルの文字コードを推定するコマンドを調べた。

環境

os: maxOS Sierra Version 10.12.6
terminal: iTerm2 Build 3.1.5

内容

ターミナル
$ file --mime tmp.txt
tmp.txt: text/plain; charset=utf-8

tmp.txtのcharsetがutf-8であることが推定された。

詳細

文字コードを推定するためのファイルを作る

ターミナル
$ cat <<EOF > tmp.txt
heredoc> こんにちは
heredoc> 世界
heredoc> EOF
$ cat tmp.txt
こんにちは
世界

tmp.txtというファイルを作成した。

文字コードを推定する

ターミナル
$ file --mime tmp.txt
tmp.txt: text/plain; charset=utf-8

tmp.txtのcharsetは、utf-8となっていると推定された。
(charsetとは、character setのことで、コンピューターの文字コード系のことらしい?[2])

fileコマンドの--mimeオプションの確認

ターミナル
$ file --help
Usage: file [OPTION...] [FILE...]
Determine type of FILEs.
(中略)
  -I, --mime                 output MIME type strings (--mime-type and
                               --mime-encoding)
(以下略)

--mimeは、MIMEタイプの文字列を出力するためのオプションらしいと分かった。
(今回は、--mime-typetext/plainで、--mime-encodingutf-8のようだ)

マイム【MIME】[multipurpose Internet mail extensions]
電子メールで、文字のほか、画像や音声、動画などさまざまなデータを転送するための規格。[3]

意義

文字コードを推定の仕方が分かった。

おまけ

同じ環境で、半角英数字のみでファイルを構成すると、us-asciiと推定される。

ターミナル
$ cat <<EOF >tmp.txt
heredoc> hello
heredoc> 123
heredoc> EOF
$ file --mime tmp.txt
tmp.txt: text/plain; charset=us-ascii

これは、utf-8が半角英数字ではus-asciiと同じエンコーディングを採用しているからではないかと思っています。(関連:[4])

参考文献

[1]http://fukuyama.co/file-i-mime
[2]https://eow.alc.co.jp/search?q=character+set&ref=wl
[3]https://kotobank.jp/word/MIME-8967
[4]https://ja.wikipedia.org/wiki/UTF-8

2
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
1