Skip to content
shellmap

fileIdentify a file by its magic bytes, not its extension across all 5 shells

Equivalents in every shell

Bashunix
file image.bin
Zshunix
file image.bin
Fishunix
file image.bin
PowerShellwindows
# No native equivalent — install via `winget install file` or use Get-Content/[byte[]] checks.

Windows has no built-in magic-byte sniffer. PowerShell can read the first bytes manually (`Get-Content -Encoding Byte -TotalCount 8 file`) and match them, but there is no one-liner.

cmd.exewindows
# No native equivalent. Install via Git Bash, WSL, or a third-party package.

cmd.exe has no equivalent. The closest built-in is `assoc` / `ftype`, which only inspects the file-extension association, not the actual contents.

Worked examples

Identify what an extension-less file actually is

Bash
file -- mystery_blob
Fish
file -- mystery_blob

Print just the MIME type, suitable for HTTP Content-Type

Bash
file --mime-type -b photo.heic
Zsh
file --mime-type -b photo.heic

Scan a directory for files whose contents disagree with their extension

Bash
for f in *; do file -b "$f"; done
Fish
for f in *; file -b "$f"; end

Gotchas

  • `file` reads magic bytes, not the filename — `file photo.jpg` will say `PNG image` if someone just renamed a PNG. That is the whole point; don't expect it to honor the extension.
  • Output text is not stable across `libmagic` versions. Pin the version, or parse the MIME-type form (`--mime-type -b`) which is far more stable for scripts.
  • macOS ships a slightly different `file` than Linux; the magic database (`/usr/share/file/magic`) is older and may misidentify newer formats (e.g. HEIC, AVIF). Install via Homebrew for parity.
  • On Windows there is genuinely no built-in equivalent. Common workarounds: `file.exe` from Git for Windows (`C:\Program Files\Git\usr\bin\file.exe`), WSL, or the Python `python-magic` package.
  • `file -i` (lowercase `i`) on GNU prints MIME type; on BSD `file -i` prints the *inverted* `--mime-type` flag meaning. Use `--mime-type` (long form) for portability.

WSL & PowerShell Core notes

pwshPowerShell Core has no `file` cmdlet on any platform. On Linux/macOS pwsh, the system `/usr/bin/file` is what you want. On Windows pwsh, install `file` via Git for Windows, scoop (`scoop install file`), or use WSL.
WSLWSL has the standard Linux `file` binary. To identify a Windows-side file from WSL: `file /mnt/c/path/to/blob.bin`. The check reads NTFS through DrvFs — fine for small files, slow for large ones because magic-byte sniffing only reads ~256 bytes anyway.

Related commands