---
title: 【公開】LPIC101_file_B3
tags: 
author: [Yukiko](https://image.docswell.com/user/yukiko_it)
site: [Docswell](https://www.docswell.com/)
thumbnail: https://bcdn.docswell.com/page/VEPKPR1Z78.jpg?width=480
description: 【公開】LPIC101_file_B3 by Yukiko
published: April 21, 26
canonical: https://image.docswell.com/s/yukiko_it/5VJPM3-2026-04-21-230911
---
# Page. 1

![Page Image](https://bcdn.docswell.com/page/VEPKPR1Z78.jpg)

LPIC-101 v5.0
｜ 小豆 本対 応
file コマンドと種別判定
「テキスト？バイナリ？ディレクトリ？」を一発で見抜く
新卒・文系・未経験のための IT 研修
4段構造で原理原則まで落とし込む30分レッスン
うさうさラーメン店方式


# Page. 2

![Page Image](https://bcdn.docswell.com/page/27VV2RYM7Q.jpg)

PROBLEM
今日のお題 ― B問題セット 問3
ファイルの種類（テキスト、バイナリ、ディレクトリ等）を判定して表示するコマンドはどれですか。
1
type
2
magic
3
file
4
stat
5
identify
※ 単一選択（正解は1つ）
LPIC-101 ｜ file コマンドとファイル種別
2 / 10


# Page. 3

![Page Image](https://bcdn.docswell.com/page/5JGLRM9X7L.jpg)

① この用語
fileコマンドって、なに？
用語
file コマンド
file type detector ／ 種別判定コマンド
ファイルを開けずに中身の種類を判定するコマンド。
拡張子は信用せず、
ファイル先頭の特徴（マジックナンバー）で判断する。
うさうさラーメン店で例えると
謎の麺 が厨房に届いた…
店長: 「これ、手打ち？乾麺？」
↓
テキスト / バイナリ / ディレクトリ を一発で見抜く。
鑑定人(file)が一口だけ食べる：
「断面のコシ→手打ち麺！」
名前じゃなく中身で判定
LPIC-101 ｜ file コマンドとファイル種別
3 / 10


# Page. 4

![Page Image](https://bcdn.docswell.com/page/47QYVRW5EP.jpg)

② 簡単に
書式は「file ファイル名」だけ
基本の書式
file
ファイル名 …
$ file /etc/passwd
→ ASCII text
テキスト
$ file /bin/ls
→ ELF 64-bit LSB executable, x86-64, …
バイナリ
$ file /etc
→ directory
ディレクトリ
★ 覚え方: 「file のあとにファイル名」だけ。拡張子の偽装にも騙されない。
LPIC-101 ｜ file コマンドとファイル種別
4 / 10


# Page. 5

![Page Image](https://bcdn.docswell.com/page/KE4WM8KVJ1.jpg)

③ 詳しく①
仕組み ― マジックナンバー方式
なぜ見抜ける？
参照先のデータベース
ファイルの先頭数バイトには、種類ごとの
「合言葉」が入っている。
/usr/share/magic
処理の流れ
代表例
7F 45 4C 46
→ ELF実行ファイル
25 50 44 46
→ PDF
89 50 4E 47
→ PNG画像
50 4B 03 04
→ ZIP / docx
この対応表が マジックデータベース
LPIC-101 ｜ file コマンドとファイル種別
1
ファイルの先頭バイトを読む
2
/usr/share/magic と照合
3
種類を画面に出力
※ 判定に失敗すると「data」と出る
5 / 10


# Page. 6

![Page Image](https://bcdn.docswell.com/page/L71Y82X4JG.jpg)

③ 詳しく②
似ているコマンドとの使い分け（4兄弟）
コマンド
何を見るか
例
出力イメージ
file
ファイルの中身の種類
file /bin/ls
ELF 64-bit ...
type
シェル内でのコマンドの種別
type ls
ls is aliased to ...
stat
ファイルの属性(inode等)
stat /etc/passwd
Size: 2554
画像ファイル専用(ImageMagick)
identify a.png
a.png PNG 800x600 ...
identify
Access: ...
★ 混同注意: type=シェル用／stat=属性／identify=画像限定／magicはコマンドでなくDB名。
LPIC-101 ｜ file コマンドとファイル種別
6 / 10


# Page. 7

![Page Image](https://bcdn.docswell.com/page/G7WGZRNZE2.jpg)

③ 詳しく③
実際の出力パターン ― 拡張子に騙されない
ファイル名が *.txt でも中身がバイナリなら file はバイナリと判定する。
Terminal ― bash
$ file /etc/passwd
/etc/passwd: ASCII text
$ file /bin/ls
/bin/ls: ELF 64-bit LSB pie executable, x86-64, ...
$ file /etc
/etc: directory
$ file report.pdf
report.pdf: PDF document, version 1.7
$ file photo.png
photo.png: PNG image data, 800 x 600, 8-bit/color RGB
$ file fake.txt
fake.txt: ELF 64-bit executable (← 実はバイナリ!)
LPIC-101 ｜ file コマンドとファイル種別
テキスト
バイナリ
ディレクトリ
PDF
PNG画像
偽装発覚
7 / 10


# Page. 8

![Page Image](https://bcdn.docswell.com/page/4JZL1R3LE3.jpg)

APPLY
原理原則で5択を斬る
#
コマンド
判定
1
type
× 誤り
シェル組み込み。「コマンドの種別」を調べるもの（別名か外部か等）
2
magic
× 誤り
これは DB の名前。コマンドではない（file が /usr/share/magic を読む）
3
file
○ 正解
ファイル中身を検査して種類を判定・表示するコマンド
4
stat
× 誤り
サイズ・inode・時刻などの「属性」を表示。種類判定ではない
5
identify
× 誤り
ImageMagick付属。画像ファイル専用の詳細表示
LPIC-101 ｜ file コマンドとファイル種別
理由
8 / 10


# Page. 9

![Page Image](https://bcdn.docswell.com/page/YE6WLRXMEV.jpg)

ANSWER
正解 ― ファイルの中身を読む唯一のコマンド
正解 ③
file
/usr/share/magic を参照して、ファイルの先頭バイトから種類を判定
覚え方 ― 「何を見るコマンド？」で覚える
file
stat
type
中身
属性
シェル
種類を判定
サイズ・時刻・inode
コマンドの種別
LPIC-101 ｜ file コマンドとファイル種別
9 / 10


# Page. 10

![Page Image](https://bcdn.docswell.com/page/GE5M18XQE4.jpg)

④ 原理原則・一言でまとめ
fileコマンドは
「名前ではなく中身」でファイルを見抜く。
中身
属性
種別
file
stat
type
参照するDBは /usr/share/magic。先頭バイトの合言葉で判定。
面白きなき世を面白く ─ 石黒 友季子


