The Linux file command: identify the file type

Introduction

In the day-to-day of a system manager or developer, it is often found with files whose type is not evident only by name or extension. The commandfileLinux quickly identifies the type of data that contains a file, based on its actual content and not on its name.

What does the file command do?

The commandfileperform a series of magic numbers and check your signature database to determine if a file is a text document, an image, an executable, a compressed file, etc. Your output is a legible description that helps you decide the next step to take.

Basic syntax

The easiest use is:

file nombre_del_archivo

Several files can be passed at the same time:

file archivo1 archivo2 archivo3

It also works with standard input by using-as a file name.

Most useful options

  • -b: shows only the description, without the file name.
  • -i: output type MIME (e.g.,text/plain; charset=us-ascii).
  • -L: follows symbolic links and analyzes the file to which they point.
  • -z: search inside compressed files to identify their content.
  • -f archivo: Read the names of the files to be analyzed from a list file.

Practical examples

Identify an image:

file foto.jpg

Typical output:

foto.jpg: JPEG image data, JFIF standard 1.01, aspect ratio, density 1x1, segment length 16, baseline, precision 8, 1920x1080, components 3

Determine the MIME type of a document:

file -i informe.pdf

Output:

informe.pdf: application/pdf; charset=binary

Analyze a compressed file without uncompressing it:

file -z backup.tar.gz

Output:

backup.tar.gz: gzip compressed data, was "tar.archive", last modified: Mon Sep 30 12:00:00 2025, max compression, OS: Unix

Use the option-bto get only the description:

file -b script.sh

Output:

Bourne-Again shell script, ASCII text executable

Interpretation of the exit

The output offileusually consists of two parts separated by two points: the name of the file and the description. When used-bor the output is processed by scripts, it is useful to extract only the second part to make automated decisions.

Limitations and advice

AlthoughfileIt's very reliable, it's not infallible. Very small files or with ambiguous signatures can be misclassified. In such cases, combinefilewith other tools likehexdumporstringscan provide more clarity. In addition, keep the signature database up to date (packagefilein distribution) ensures the detection of new file types.

Conclusion

The commandfileis an essential tool for any Linux user who needs to know quickly what type of data it handles. Its simplicity, speed and wealth of options make it an indispensable ally in management, cleansing and automation.

This work is under aCreative Commons License Attribution 4.0 International for Francesc Roig francesc @ vivaldi.net.

EnglishenEnglishEnglish