Skip to main content

Command Palette

Search for a command to run...

Cheatsheet & Examples: file

Updated
3 min read

The file command identifies the type of a file or files by checking their contents and metadata.

Identify the type of a single file

Example Usage:
file filename

What it does:
Determines the type of the specified file, such as text, image, executable, or compressed data.

Command-line Arguments Explained:

  • filename: The file whose type is to be identified.

Display brief output without filename

Example Usage:
file -b filename

What it does:
Shows only the file type information without including the filename in the output, useful for scripting.

Command-line Arguments Explained:

  • -b: Use brief output (suppresses filename display).
  • filename: The file whose type is to be identified.

Identify the type of multiple files

Example Usage:
file file1 file2 file3

What it does:
Checks the file types of all specified files, displaying each result individually.

Command-line Arguments Explained:

  • file1, file2, file3: List of files to analyze.

Display MIME type information

Example Usage:
file -i filename

What it does:
Reports the MIME type (e.g., text/plain, image/jpeg) of the file, which is useful for web and email applications.

Command-line Arguments Explained:

  • -i: Output the MIME type and subtype.
  • filename: The file to analyze.

Example Usage:
file -L symlink

What it does:
Resolves symbolic links and checks the type of the file they point to, rather than the link itself.

Command-line Arguments Explained:

  • -L: Follow symbolic links to their target files.
  • symlink: The symbolic link to evaluate.

Check block or character device files

Example Usage:
file -s /dev/device

What it does:
Analyzes the type of special device files (e.g., /dev/null, /dev/sda) such as block or character devices.

Command-line Arguments Explained:

  • -s: Check block or character device files (requires read access to the device).
  • /dev/device: The device file to analyze.

Read filenames from a list file

Example Usage:
file -f list.txt

What it does:
Uses the filenames listed in list.txt to determine their types, processing them one by one.

Command-line Arguments Explained:

  • -f: Read filenames from the specified file (list.txt).
  • list.txt: A text file containing the names of files to analyze.

Use a custom magic file for classification

Example Usage:
file -m /path/to/magicfile filename

What it does:
Overrides the default magic database with a custom one to identify the file type based on user-defined rules.

Command-line Arguments Explained:

  • -m: Specify the path to a custom magic file.
  • /path/to/magicfile: The location of the custom magic database.
  • filename: The file to analyze.

Continue checking after errors

Example Usage:
file -k filename

What it does:
Keeps processing other files even if one encounters an error (e.g., permission denied).

Command-line Arguments Explained:

  • -k: Continue checking after errors.
  • filename: The file to analyze.

More from this blog

Hong's Tech Blog

110 posts

The blog is older than you know. I prefer counting from the emergence of one integral anomaly to the emergence of the next, in which case this is the forth version.