Home > File & Directory Management > file

file: Guide to Identifying File Types

Determines the type and MIME information of files by examining their actual byte content instead of relying on extensions. It uses the libmagic database.

Overview

The `file` command determines the type of a file by analyzing its content, rather than relying on its extension. It recognizes various formats such as text/binary, executable file formats (ELF), compressed files, images/audio, and can also output MIME types and encodings.

Key Features

  • Determines type based on actual content, regardless of extension
  • Can output MIME type and character encoding
  • Can choose to identify the symbolic link target or the link itself
  • Attempts to check inside compressed files (-z)

Example Usage Scenarios

Situations where the `file` command particularly shines.

  • Identifying the actual format of a file with a misleading extension (e.g., a PDF disguised as a .jpg)
  • Determining the type of files without extensions
  • Branching processing in scripts based on file type
  • Determining MIME types of uploaded files in web/network services
  • Identifying the symbolic link itself or its target
  • Analyzing content inside compressed files
  • Identifying executable file formats in security analysis and forensics

Key Options

Commonly used options grouped by scenario.

1) Basic Operation & Output Control

2) Input/Target Specification

3) Magic Database/Exclusion/Debug

4) Help/Version

Generated command:

Try combining the commands.

Description:

`file` Executes the command.

Combine the above options to virtually execute commands with AI.

Usage Examples

Common patterns are organized as examples.

Basic Identification

file /bin/ls

Outputs file type in basic format

Brief Output Without Filename

file -b /bin/ls

Simple output, easy to process in scripts

MIME Type/Encoding

file --mime-type index.html && file --mime-encoding README.md

Useful for type identification in web/pipelines

Check Inside Compressed Files

file -z backup.tar.gz

Estimates the actual content of compressed files

Symbolic Link Itself vs. Target

file -h symlink && file -L symlink

Examine the link itself (-h) or follow the target (-L)

Bulk MIME Type Check with find

find . -type f -print0 | xargs -0 file --mime-type

Safely passes file lists using NUL delimiters

Magic Database Location

Paths may vary depending on the system and distribution.

🪄 Default Compiled Magic DB

/usr/share/file/magic.mgc

Default location for most distributions

📁 Source Magic Directory

/usr/share/file/magic/

Source rules before compilation (original, not .mgc)

Exit Codes

Refer to these when handling conditional logic in scripts.

Code Meaning
0 Success: Identification successful (or requested operation successful)
1 General problem: File not found/inaccessible/identification failed, etc.
2 Usage error or system error

Installation

Mostly pre-installed, but if not, install with the following:

Debian/Ubuntu

sudo apt update && sudo apt install -y file

RHEL/CentOS/Fedora

sudo dnf install -y file

Arch Linux

sudo pacman -S --needed file

openSUSE

sudo zypper install -y file

Tips & Cautions

  • Using `--mime-type` and `--mime-encoding` explicitly can avoid differences in `-i/--mime` behavior across distributions/versions.
  • Symbolic link handling: Use `-h` to examine the link itself, and `-L` to check the target file.
  • When processing in bulk, using the `find -print0 | xargs -0` pattern ensures safety with filenames containing spaces/newlines.
  • Depending on the compression format, `-z` may not always be able to check inside. If necessary, decompress first to check.

Related commands

These are commands that are functionally similar or are commonly used together.


Same category commands