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?

Linuxコマンド入門:file コマンドの基本とファイルの種類の調べ方、オプションについてまとめてみた

Posted at

はじめに

Linuxではさまざまなファイルタイプが存在し、それを適切に扱うためには種類を把握することが重要です。

そこで役立つのが file コマンドです。

本記事では、file コマンドの基本的な使い方や主なオプションについて簡潔に解説します。

書こうと思ったきっかけ

ファイルの種類を知る際、拡張子だけでは判断できないことがありました。

また、バイナリファイルとテキストファイルを見分ける方法を知りたい場面が多かったため、file コマンドの活用方法をまとめることにしました。

file コマンドとは?

file コマンドは、拡張子に依存せずファイルの中身を解析し、種類を特定するコマンドです。

基本的な使い方

ファイルの種類を調べる

file filename

例:

file example.txt
example.txt: ASCII text

このように、ファイルの種類が判定されます。

実際のターミナル画面

Screenshot 2025-03-13 at 21.24.10.png

file コマンドの主なオプション

-i オプション(MIMEタイプを表示)

file -i filename

例:

file -i example.txt
example.txt: text/plain; charset=us-ascii

実際のターミナル画面

Screenshot 2025-03-13 at 21.36.15.png

-b オプション(ファイル名を表示しない)

file -b filename

例:

file -b example.txt
ASCII text

-L オプション(シンボリックリンクを追跡)

file -L symlink

-z オプション(圧縮ファイルの中身を判定)

file -z archive.gz

file コマンドの応用例

ディレクトリ内のすべてのファイルの種類を確認

file *

実際のターミナル画面

Screenshot 2025-03-13 at 21.37.53.png

バイナリファイルかテキストファイルかを判別

file -i filename | grep -q 'charset=binary' && echo "バイナリファイル" || echo "テキストファイル"

まとめ

file コマンドは、拡張子に頼らずファイルの種類を特定する便利なコマンドです。

MIMEタイプの確認や圧縮ファイルの解析、シンボリックリンクの追跡など、多くの場面で役立ちます。

適切なオプションを活用し、効率よくファイルを管理してみてください!

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?