File Signatures (Magic Numbers): Identify Any File Type
By SimpleTools24Published 4 min read
A file signature, also called a magic number or magic bytes, is a fixed sequence of bytes, usually at the very start of a file, that identifies its format. A PNG image always begins with 89 50 4E 47 0D 0A 1A 0A, a PDF with 25 50 44 46 (%PDF), and a ZIP archive with 50 4B 03 04 (PK). Programs read these bytes to decide how to open a file, which is why the signature is far more reliable than the file extension.
In this guide
Why Signatures Beat Extensions
An extension is part of the file's name, and anyone can change it. A signature is part of the content. That makes signatures useful when:
- A download has no extension or the wrong one.
- A file was renamed (a
.jpgthat's really a WebP, a.7zthat's really a RAR). - You suspect a file is disguised, such as an executable posing as a document.
- An app reports a corrupt file and you want to check it's even the right format.
Operating systems handle this differently. Linux and macOS have the file command, which uses a database of signatures (libmagic). Windows mostly trusts the extension, which is one reason disguised files are a common trick.
Common File Signatures
| Format | Hex signature | As text | Offset |
|---|---|---|---|
| PNG image | 89 50 4E 47 0D 0A 1A 0A | .PNG.... | 0 |
| JPEG image | FF D8 FF | ... | 0 |
| GIF image | 47 49 46 38 37 61 or 47 49 46 38 39 61 | GIF87a / GIF89a | 0 |
| WebP image | 52 49 46 46 ?? ?? ?? ?? 57 45 42 50 | RIFF....WEBP | 0 |
| PDF document | 25 50 44 46 2D | %PDF- | 0 |
| ZIP (also DOCX, XLSX, PPTX, JAR, APK, EPUB) | 50 4B 03 04 | PK.. | 0 |
| RAR archive | 52 61 72 21 1A 07 | Rar!.. | 0 |
| 7z archive | 37 7A BC AF 27 1C | 7z.... | 0 |
| gzip | 1F 8B | .. | 0 |
| MP3 with ID3 tag | 49 44 33 | ID3 | 0 |
| WAV audio | 52 49 46 46 ?? ?? ?? ?? 57 41 56 45 | RIFF....WAVE | 0 |
| FLAC audio | 66 4C 61 43 | fLaC | 0 |
| MP4 / MOV video | 66 74 79 70 | ftyp | 4 |
| SQLite database | 53 51 4C 69 74 65 20 66 6F 72 6D 61 74 20 33 00 | SQLite format 3. | 0 |
| Windows executable | 4D 5A | MZ | 0 |
| Linux executable (ELF) | 7F 45 4C 46 | .ELF | 0 |
| DICOM medical image | 44 49 43 4D | DICM | 128 |
?? means "any byte": in RIFF-based formats those four bytes store the file size, so they vary.
Reading the Table Correctly
A few details trip people up:
The Offset Matters
Not every signature sits at byte 0. MP4 files start with a four-byte box size, and ftyp follows at offset 4. DICOM files have a 128-byte preamble (often all zeros) before DICM. If you only check the first bytes, you'll miss these. A DICOM viewer relies on exactly that check.
Container Formats Share Signatures
Office documents (DOCX, XLSX, PPTX), Java archives, Android apps and EPUB books are all ZIP files internally, so they all start with PK. To tell them apart you need to look inside: a DOCX contains a word/ folder, an XLSX a xl/ folder.
Some Formats Have No Signature
Plain text, CSV, many MP3 files without ID3 tags, and lots of raw data formats have no fixed header. Identifying them takes heuristics, such as whether the bytes look like valid UTF-8 or match MP3 frame patterns. "Unknown" is a normal answer, not an error. Binary vs text files explains how tools tell text apart from binary data.
Signatures Aren't Proof
A signature tells you what the file claims to be at the start. A truncated or corrupted file can have a perfect signature and still fail to open, and a malicious file can carry a valid header. Treat it as identification, not validation.
How to Check a File's Signature
On macOS and Linux
file mystery.bin
xxd mystery.bin | head -n 2file names the format if libmagic recognises it; xxd shows the bytes so you can compare them with the table.
On Windows
Format-Hex -Path .\mystery.bin | Select-Object -First 2In the Browser
You can check a file's signature online without uploading it. Open the file and the File info tab compares its leading bytes with about 30 common signatures, including PNG, JPEG, PDF, ZIP, GIF and ELF, and reports any match along with its offset. The hex view shows the bytes themselves if you want to compare them manually. If you're new to reading those bytes, how to read a hex dump is a good primer.
Practical Examples
- "The archive is corrupt." If a
.7zfile starts with52 61 72 21, it's actually a RAR; open it with a RAR opener instead. More archive errors are covered in fixing "Cannot open file as archive". - "This image won't open." A
.jpgbeginning withRIFF....WEBPis a WebP image; rename it or open it with a WebP-capable app. - "Downloaded PDF shows nothing." If the file begins
<!DOCTYPE html, the server sent an error page, not a PDF. - "Invoice.pdf.exe." A file starting with
MZis a Windows program, whatever its name suggests. Don't run it.
Frequently Asked Questions
Why is it called a magic number?
The term comes from early Unix, where executable files began with a numeric constant the system checked before running them. The name stuck for any identifying byte sequence.
Can I change a file's type by editing its signature?
No. Changing the first bytes just makes the file unreadable. The rest of the data still follows the original format.
Do text files have signatures?
Not usually. Some start with a byte order mark (EF BB BF for UTF-8), but that indicates encoding, not format.
Is checking the signature enough to know a file is safe?
No. It tells you the format, not whether the content is harmless.
Summary
Look at the first few bytes, and at known offsets like 4 or 128, to identify a file regardless of its name. Keep a reference list handy, and remember that ZIP-based formats share one signature.
Related tools
Sources
- PNG Specification, Third Edition – W3C (opens in a new tab)
- file(1) and libmagic – Fine Free File Command (opens in a new tab)
Related articles
- How to Read a Hex DumpA line-by-line walkthrough of offsets, hex bytes, the ASCII column and little-endian numbers.
- How to View a Binary FileSee inside .bin, .dat and unidentified files with hex viewers, built-in commands and a browser-based tool.
- Fix “Cannot Open File as Archive”What 7-Zip's archive errors mean and how to fix incomplete downloads, wrong formats, split parts and damaged data.