file Command in Linux



The file command in Linux determines the file type. It performs a sequence of tests for each argument. These tests include the filesystem test, magic test, and language test. The first successful test determines the file type.

The filesystem test uses a stat system call to identify the attributes of a file, such as size and other parameters. The magic test identifies a file's magic number and determines the file type. The magic number is the byte sequence found at the beginning of the file, indicating the file format.

The language test identifies file types based on specific patterns and signatures. It searches for particular strings that can appear in the first few blocks of a file to identify the file type.

Table of Contents

Here is a comprehensive guide to the options available with the file command −

Syntax of file Command

The syntax of the Linux file command is as follows −

file [options] [filename]

The [options] field is used to specify the options to modify the command’s behavior. The [filename] option is used to specify the filename whose type is needed to be determined.

file Command Options

The options used with the file command are listed below −

Flags Options Description
-b --brief It is used to skip the filename from the output
-C --compile It is used to compile file specified by -m
-c --checking-printout It is used to print the parsed form of a magic file, used with -m to debug a magic file before installing
-d --debug It is used to print the debugging messages
-e testname --exclude testname It is used to exclude a specific test to be performed; valid tests are: apptype, ascii, cdf, compress, csv, elf, encoding, soft, tar, json, simh, text, tokens
-F string --separator string It is used to specify a string to be used as a separator instead of a colon (:)
-f file --files-from file It is used to read the files from the specified file to determine the file type
-h --no-dereference It is used to skip symlinks (default)
--help It is used to display help related to command
-i --mime It is used to display mime type and mime-encoding instead of human-readable form
--apple It is used to display output in Apple CREATOR/TYPE format
--extension It is used to display the slash-separated list of extensions
--mime-type It is used to display the mime-type
--mime-encoding It is used to display mime encoding
-k --keep-going It is used to tell the command not to stop at the first match
-L --dereference It is used to follow the symlinks
-m files --magic-file files It is used to specify alternative magic files and directories
-N --no-pad It is used to display output without padding
-n --no-buffer It is used to prevent the output buffer
-p --preserve-date It is used to preserve access times on files
-P --parameter It is used to set the engine parameter limits
-r --raw It is used to avoid unprintable characters to \ooo translation
-s --special-files It is used to treat the special files as ordinary
-S --no-sandbox It is used to disable a system call sandboxing
-v --version It is used to print the command version
-z --uncompress It is used to look inside the compressed files
-Z --uncompress-noreport It is used to print the contents of compressed files
-0 --print0 It is used to terminate the filenames with ASCII null

Examples of file Command in Linux

This section demonstrates the usage of the Linux file command through various examples −

  • Displaying File Type
  • Displaying File Type without Filename
  • Displaying the Debugging Message
  • Displaying Additional Information on a Block Device
  • Displaying the MIME Type and MIME Encoding
  • Excluding a Test Type

Displaying File Type

To display the file type of file, use the file command without any option −

file test
Displaying File Type 1

It is a useful tool to determine the file type when the extension is not mentioned.

In the example, the file has no extension, but the file command identifies it as an executable, as shown in the output image.

Displaying File Type 2

Similarly, other file types can be determined.

Displaying File Type without Filename

By default, the filename comes with the file type in the file command standard output. To skip the filename, use the -b or --brief option −

file -b test
Displaying File Type without Filename

Displaying the Debugging Message

To display the debugging messages with the output, use the -d or --debug option −

file -d test

Displaying Additional Information on a Block Device

By default, determining the file type of a block device file will output a simple file type. To treat the block device file as an ordinary file, the -s or --special-file option is used −

sudo file -s /dev/vda
Additional Information on Block Device

As an ordinary file, the block device file will be analyzed for further information.

Displaying the MIME Type and MIME Encoding

To display the file type in MIME format, use the -i or --mime option −

file -i test
MIME Type and MIME Encoding 1

The charset in the output indicates the MIME encoding.

MIME stands for Multipurpose Internet Mail Extensions and is used to specify the nature of a file or data. For example, a text will be displayed as text/plain, and an image as image/png.

Note that the MIME type and MIME encoding can be displayed separately using the --mime-type and --mime-encoding options:

file --mime-type test
file --mime-encoding test
MIME Type and MIME Encoding 2

Excluding a Test Type

It should be "To exclude a specific test from determining the file type, use the -e or --exclude option. For example, to exclude the text test, use −

file -e text test
Excluding Test Type

Other test types that can be excluded are apptype, ascii, cdf, compress, csv, elf, encoding, soft, tar, json, simh, tokens.

Conclusion

The file command in Linux is used to determine the file type by performing a sequence of tests. These tests include filesystem, magic, and language tests. It is a handy tool to determine file types without relying on the extensions.

In this tutorial, we explained the file command, its syntax, options, and usage in Linux through various examples.

Advertisements