file Command in Linux: Determine File Types

By 

Updated on

4 min read

Terminal example showing the Linux file command identifying 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:

txt
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:

Terminal
file /etc/hostname
output
/etc/hostname: ASCII text

To print only the type without the file name, use the -b (--brief) option:

Terminal
file -b /etc/hostname
output
ASCII text

The 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:

Terminal
file /bin/bash /etc/hostname /usr/share/icons/hicolor/48x48/apps/firefox.png
output
/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-interlaced

Shell wildcards also work. For example, to check every .conf file in /etc:

Terminal
file /etc/*.conf

Reading 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:

Terminal
file -f filelist.txt

The 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:

Terminal
file -i /var/www/html/index.html
output
/var/www/html/index.html: text/html; charset=us-ascii

This 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:

Terminal
file -z archive.gz
output
archive.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”.

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:

Terminal
file /usr/bin/python3
output
/usr/bin/python3: symbolic link to python3.12
Terminal
file -L /usr/bin/python3
output
/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, stripped

How 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:

Terminal
file --debug /etc/hostname

You 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

TaskCommand
Check file typefile document.pdf
Type only (no filename)file -b document.pdf
MIME typefile -i document.pdf
Inside compressed filefile -z archive.gz
Multiple filesfile *.log
Read list from filefile -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.

Linuxize Weekly Newsletter

A quick weekly roundup of new tutorials, news, and tips.

About the authors

Dejan Panovski

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