file Command

Purpose

Checks the file contents and magic patterns and determines the file type.

Syntax

file [ -b ] [ -c ] [ -i ] [ -h ] [ -m magicfile ] [ file ... ]

Description

The file command tests each specified file and attempts to classify it by consulting the magic database of the system. If no file is specified or if file is specified as -, standard input is examined. In this restricted environment, only read-only analysis is supported.

Flags

Table 1. Flags
Item Description
-b Displays file content in brief. This option does not prefix the file names to output lines.
-c Checks the format of the magic file for errors. This option does not examine the files.
-i Displays Multipurpose Internet Mail Extensions (MIME) type strings instead of traditional descriptions.
-m magicfile Uses an alternative magic file instead of the system default file.
-h Displays help about the file command.

Examples

  1. To identify the type of a single file, enter the following command:
    file /etc/hosts file_path
  2. To display file content in brief for multiple files, enter the following command:
    file -b file_path1 file_path2
  3. To display MIME type strings, enter the following command:
    file -i file_path
  4. To check the syntax of the magic file, enter the following command:
    file -c file_path