0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

マジックナンバー ってなに?

Posted at

勉強前イメージ

わからん!

調査

マジックナンバー とは

マジックナンバーとはファイルの先頭に記載されている識別子で、これをみることによってファイルの種類を判別することが出来ます。
先頭数バイトで記載されていて、そこに記載されている数値で判別します。
もちろん空のファイルとかだと識別子がない場合もあります。

そのマジックナンバー(識別子)でファイルの種類を確認する方法が fileコマンド になります。
fileコマンドは見たい対象のファイルの識別子を確認し、マジックナンバーのリストと照らし合わせて判別します。

例えば、シェルスクリプトなら以下のように表示されます。

[root@localhost ~]# file test.sh 
test.sh: Bourne-Again shell script, ASCII text executable

中が何も書かれていないファイルであれば以下のように empty と表示されます。

[root@localhost ~]# touch test1.sh
[root@localhost ~]# file test1.sh 
test1.sh: empty

fileコマンドで照らし合わせるマジックナンバーのリストはディストリビューションによっても違いますが
CentOS7 だと /usr/share/file/magic になります。

拡張子とマジックナンバーが違ったら?

  • textファイルを用意する

ASCII text と表示されました。

[root@localhost ~]# cat test.txt 
test
[root@localhost ~]# file test.txt 
test.txt: ASCII text
  • textファイルの中身をシェルスクリプトに変更

textファイルの拡張子で中身をシェルスクリプトにすると
fileコマンドではシェルスクリプトと判定されました。
これはファイルの中身(先頭)を確認して判定しているので、拡張子によらず中身が変わればfileコマンドでも判定が変わるようです。

[root@localhost ~]# cat test.txt 
#!/usr/bin/bash
echo "test"
[root@localhost ~]# file test.txt 
test.txt: Bourne-Again shell script, ASCII text executable

勉強後イメージ

拡張子の意義ってなんだろう・・・わかんなくなってきた

参考

0
0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?