file

file linux command cheatsheet by Thamizhiniyan C S

Introduction

The man command is used to determine the file type.


Syntax

file [-bcdEhiklLNnprsSvzZ0] [--apple] [--exclude-quiet] [--extension] [--mime-encoding] [--mime-type] [-e testname] [-F separator] [-f namefile] [-m magicfiles] [-P name=value] file ...

file -C [-m magicfiles]

file [--help]


Important Flags

FlagDescription

--apple

Changes the command output to the one used by older versions of MacOS.

-b, --brief

Changes command output to brief mode.

-C, --compile

Creates an output file that contains a pre-parsed version of the magic file or directory.

-c, --checking-printout

Checks the printout for the parsed version of the magic file.

-d

Prints internal debugging information in the standard error format.

-E

On filesystem error, issues an error message and exits.

-e, --exclude

Excludes a test from the list of tests performed on a file.

--exclude-quiet

Excludes tests that the file command doesn't know about.

--extension

Prints a list of valid extensions for the file type.

-F, --separator

Uses the provided string as a separator between the file name and file type.

-f, --files-from

Uses a provided text file as a list of files to test. The list must contain only one file name per line.

-h, --no-deference

Disables following symbolic links.

-i, --mime

Changes the command output to a MIME-type string.

--mime-type, --mime-encoding

Changes the command output to a MIME-type string and only displays the specified element (type or encoding).

-k, --keep-going

Keeps the test going after the first results match.

-l, --list

Shows a list of matching patterns in descending order of strength.

-L, --deference

Enables following symbolic links.

-m, --magic-file

Uses an alternative magic file provided by the user.

-N, --no-pad

Doesn't pad the file names to align with the output.

-n, --no-buffer

Flushes the output after checking each file.

-p, --preserve-date

Attempts to preserve the last time the file was accessed to make it look like the file command didn't test it.

-P, --parameter

Sets various parameters, such as max bytes or recursion, count, and length limit.

-r, --raw

Disables translating unprintable characters.

-s, --special-files

Enables reading special files.

-S, --no-sandbox

Disables sandboxing on systems that support it.

-v, --version

Displays the version of the file command.

-z, --uncompress

Checks compressed files.

-Z, --uncompress-noreport

Checks compressed files and only displays file type without the compression.

-0, --print0

Displays a null character after the end of the file name.

--help

Displays the help message.


Examples

CommandDescription

To find the correct type of a file named 'filename'.

Test multiple files simultaneously.

View the file type in brief mode for a file named 'text.zip'.

Display brief version of file type output.

List each file type in the current directory.

List each file type inside a specific directory.

View the MIME file type of a file named 'filename'.

Check detailed information and content of a compressed file like ZIP or gzip.

Test a compressed file to detect its content.

Display only the file type of a compressed file like ZIP or gzip.

View the parsed version of a file, useful for debugging magic files.

Display the check printout for the parsed version of a file.

List all file types within specified Regex-style ranges.

Test files in a directory within the range of names from 'a' to 'l'.

List all file types within specified Regex-style ranges.

Test files listed in a text file (one file per line).

Test a special system file.

Fully test a special system file (requires root).

Define '+' as a separator between file name and type in output.

Remove padding between file name and type in output.

Last updated