file Command in Linux: Determine File Types

Not every file on a Linux system has an extension, and even when one is present it can be misleading. The file command inspects the actual contents of a file and reports its type, regardless of the name.
This is useful when you encounter an unfamiliar file, need to verify that a download is what it claims to be, or want to script decisions based on file type.
Syntax
The general syntax for the file command is:
file [OPTIONS] [FILE...]You can pass one or more file names as arguments.
Determining File Type
The file command runs a series of tests against each file and reports the type based on the first successful match. In its simplest form, it prints the file name followed by the detected type:
file /etc/hostname/etc/hostname: ASCII textTo print only the type without the file name, use the -b (--brief) option:
file -b /etc/hostnameASCII textThe output tells you that /etc/hostname is a plain text file.
Checking Multiple Files
You can pass more than one file to the file command. It prints a result for each one:
file /bin/bash /etc/hostname /usr/share/icons/hicolor/48x48/apps/firefox.png/bin/bash: ELF 64-bit LSB pie executable, x86-64, version 1 (SYSV), dynamically linked, interpreter /lib64/ld-linux-x86-64.so.2, BuildID[sha1]=33a5554034feb2af38e8c75872058883b2988bc5, for GNU/Linux 3.2.0, stripped
/etc/hostname: ASCII text
/usr/share/icons/hicolor/48x48/apps/firefox.png: PNG image data, 48 x 48, 8-bit/color RGBA, non-interlacedShell wildcards also work. For example, to check every .conf file in /etc:
file /etc/*.confReading File Names from a File
When you have a long list of paths to check, place them in a text file (one per line) and use the -f option:
file -f filelist.txtThe file command reads each path from filelist.txt and prints its type, which is convenient for batch processing.
Displaying the MIME Type
Use the -i (--mime) option to display the MIME type and character encoding instead of the human-readable description:
file -i /var/www/html/index.html/var/www/html/index.html: text/html; charset=us-asciiThis is useful in scripts that need to make decisions based on MIME type rather than parsing a free-text description.
Checking Inside Compressed Files
By default, file reports the type of the compressed file itself. The -z option tells it to also look inside and report the type of the compressed contents:
file -z archive.gzarchive.gz: ASCII text (gzip compressed data, was "notes.txt", last modified: Mon Apr 7 10:22:14 2026, from Unix, original size modulo 2^32 524)Without -z you would only see “gzip compressed data”.
Following Symbolic Links
When you point file at a symbolic link, it reports the link itself by default. To follow the link and inspect the target, use the -L option:
file /usr/bin/python3/usr/bin/python3: symbolic link to python3.12file -L /usr/bin/python3/usr/bin/python3: ELF 64-bit LSB pie executable, x86-64, version 1 (SYSV), dynamically linked, interpreter /lib64/ld-linux-x86-64.so.2, BuildID[sha1]=e3b0d5c44e8e1e4e6a77a3d64ce99e2a0b9e1f2c, for GNU/Linux 3.2.0, strippedHow file Works: The Magic Database
The file command does not rely on file extensions. It uses a set of rules called the “magic database” stored in /usr/share/misc/magic (or /usr/share/file/magic on some systems). Each rule describes a byte pattern, offset, and type label.
When you run file, it tests the file contents against these rules in order. The first matching pattern determines the reported type. You can see which rule matched by using the --debug flag:
file --debug /etc/hostnameYou can also supply your own magic file with the -m option, which is useful for identifying custom binary formats.
Common Options
-b- Print only the file type, without the file name-i- Display the MIME type and encoding-z- Look inside compressed files-h- Do not follow symbolic links (default behavior on most systems)-f FILE- Read file names from FILE, one per line-m FILE- Use FILE as the magic database-L- Follow symbolic links--debug- Show how the type was determined
Quick Reference
| Task | Command |
|---|---|
| Check file type | file document.pdf |
| Type only (no filename) | file -b document.pdf |
| MIME type | file -i document.pdf |
| Inside compressed file | file -z archive.gz |
| Multiple files | file *.log |
| Read list from file | file -f filelist.txt |
FAQ
What is the difference between file and checking the file extension?
The file command inspects the actual content of the file, not its name. A file named photo.jpg could actually be a PNG or even a text file. The file command will report the true type.
Can file identify scripts?
Yes. If a file starts with a shebang (#!/bin/bash, #!/usr/bin/env python3), file reports it as a script with the interpreter name.
Does file work on binary files?
Yes. It identifies ELF executables, shared libraries, Java class files, and many other binary formats using the magic database.
Conclusion
The file command identifies file types by inspecting content rather than relying on extensions. For related tools, see grep
for searching file contents and find
for locating files by name or attributes.
Tags
Linuxize Weekly Newsletter
A quick weekly roundup of new tutorials, news, and tips.
About the authors

Dejan Panovski
Dejan Panovski is the founder of Linuxize, an RHCSA-certified Linux system administrator and DevOps engineer based in Skopje, Macedonia. Author of 800+ Linux tutorials with 20+ years of experience turning complex Linux tasks into clear, reliable guides.
View author page