How to Determine the File Type of a File Using Linux

Find the file type of any file or group of files with the 'file' command

What to Know

  • Use the file command followed by the file name.
  • For multiple files, enter the file command followed by the target files. Or, use a text editor to create a file with the list of file names.
  • In Linux, a file's actual type can be altogether different from what its extension might imply.

Linux doesn't use file extensions; rather, the file's type is part of the file name. This article explains how to find out the true file type in Linux.

How to Use the 'file' Command

The file command takes the following form:

file filename

For example, to check the type of a file title touch.svg, execute the following command:

file touch.svg

The output is something like this:

touch.svg: SVG Scalable Vector Graphics image

Different file types produce different results, for example:

  • ODS file type: OpenDocument Spreadsheet
  • PDF file type: PDF Document, version 1.4
  • CSV file type: ASCII text, with very long lines, with CRLF line indicators

Handling Multiple Files

By default, the file command works with a single file. You can, however, specify a filename that contains a list of files to be processed by the file command.

For example, open a file called testfiles using the nano editor and add these lines to it:

  • /etc/passwd
  • /etc/pam.conf
  • /etc/opt

Save the file and run the following file command:

file -f testfiles

The output will be something like this:

  • /etc/passwd: ASCII text
  • /etc/pam.conf: ASCII text
  • /etc/opt: directory

Another method is to run the file command once, but specifying several targets. For example:

file /etc/passwd /etc/pam.conf /etc/opt

The output will be something like this:

  • /etc/passwd: ASCII text
  • /etc/pam.conf: ASCII text
  • /etc/opt: directory

Standard wildcards work, too. For example, to test all files in the present working directory, use:

file *

To test for directories that start with the letter D (case-sensitive), try this:

file D*

The results might be Desktop, Documents, and Downloads, for example.

Compressed Files

When you run the file command against a compressed file, you see output something like this:

  • ZIP archive data, at least V2.0 to extract

While this result tells you that the file is an archive file, you don't know the contents of the file. Look inside the zip file to see the file types of the files within the compressed file. The following command runs the file command against the files inside a ZIP file:

file -z filename

The output now shows the file types of files in the archive.

How the 'file' Command Works

The file command runs three sets of tests against a file:

  • Filesystem tests
  • Magic tests
  • Language tests

The first set of tests to return a valid response prompts the file type to be printed.

Filesystem tests examine the return from a stat system call. The program checks to see if the file is empty and whether it is a special file. If the file type is found in the system header file, it is returned as the valid file type.

The magic tests check the contents of a file and specifically a few bytes at the beginning that help to determine the file type. Various files are used to help match up a file with its file type, and these are stored in:

  • /etc/magic
  • /usr/share/misc/magic.mgc
  • /usr/share/misc/magic

The final tests are language tests. The file is checked to see if it is a text file. By testing the first few bytes of a file, the test deduces whether the file is an ASCII, UTF-8, UTF-16, or another format that identifies the file as a text file. When the character set is deduced, the file is tested against different languages.

Override these files by placing a file in your home folder called $HOME/.magic.mgc or $HOME/.magic.

Was this page helpful?