The file
command is used to determine the type of a file. It examines the file’s content and provides information about its format, such as whether it is a text file, a binary executable, or an image file. This command is useful for understanding the nature of files, especially when their extensions are not indicative of their content.
Initial Example
Using file
to determine the type of a file called example.txt
:
file example.txt
FILE Parameters
Parameter | Description |
---|---|
--apple | Output the file type and creator code as used by older MacOS versions |
-b, --brief | Suppress filenames in the output for a cleaner, brief format |
-C, --compile | Generate a pre-parsed magic.mgc file from the magic file or directory |
-c, --checking-printout | Print the parsed form of the magic file for debugging purposes |
-d | Print internal debugging information to stderr |
-E | Exit on filesystem errors, like file not found, instead of continuing |
-e, --exclude testname | Exclude specified tests from determining the file type (e.g., ascii , compress , json ) |
--exclude-quiet | Ignore unknown tests specified in the exclude list, for compatibility with older versions |
--extension | Display valid file extensions for the identified file type |
-F, --separator | Use a custom string as a separator between filename and file type in the output |
-f, --files-from | Read file names to be examined from the specified file, one per line |
-h, --no-dereference | Do not follow symbolic links (affect the symlink itself) |
-i, --mime | Output MIME type strings instead of human-readable file types |
--mime-type | Output only the MIME type of the file |
--mime-encoding | Output only the MIME encoding of the file |
-k, --keep-going | Continue checking for additional matches after the first match |
-l, --list | List all patterns and their strength sorted by matching strength |
-L, --dereference | Follow symbolic links to the actual file |
-m, --magic-file | Specify an alternate file or directory containing magic definitions |
-N, --no-pad | Do not pad filenames to align them in the output |
-n, --no-buffer | Flush stdout after checking each file for real-time output |
-p, --preserve-date | Preserve the access time of files during analysis |
-P, --parameter | Set various limits for file processing parameters, like maximum bytes to read, recursion limits |
-r, --raw | Do not translate unprintable characters to octal representation |
-s, --special-files | Also read and determine the type of special files (block, character devices, pipes, etc.) |
-S, --no-sandbox | Disable sandboxing to allow the execution of external decompressing programs |
-v, --version | Display version information and exit |
-z, --uncompress | Attempt to look inside compressed files |
-Z, --uncompress-noreport | Look inside compressed files but report only the contents, not the compression method |
-0, --print0 | Output a null character (\0 ) after each filename for compatibility with tools like cut |
--help | Display help information and exit |
Examples
1. Determine the Type of a Single File
To identify the type of a specific file:
file filename.txt
2. Determine the Type of Multiple Files
To identify the types of multiple files at once, specify their names separated by spaces:
file file1.txt file2.jpg file3.pdf
3. Determine the Type of All Files in a Directory
To identify the types of all files in a directory, use a wildcard (*
):
file /path/to/directory/*
4. Verbose Output
To display more detailed information about the file type, use the -v
(verbose) option:
file -v example.txt
5. Follow Symbolic Links
To follow symbolic links and determine the type of the target file, use the -L
option:
file -L symbolic_link