Linux – file Command

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.



Tutorials dojo strip

Initial Example

Using file to determine the type of a file called example.txt:

file example.txt




FILE Parameters

ParameterDescription
--appleOutput the file type and creator code as used by older MacOS versions
-b, --briefSuppress filenames in the output for a cleaner, brief format
-C, --compileGenerate a pre-parsed magic.mgc file from the magic file or directory
-c, --checking-printoutPrint the parsed form of the magic file for debugging purposes
-dPrint internal debugging information to stderr
-EExit on filesystem errors, like file not found, instead of continuing
-e, --exclude testnameExclude specified tests from determining the file type (e.g., ascii, compress, json)
--exclude-quietIgnore unknown tests specified in the exclude list, for compatibility with older versions
--extensionDisplay valid file extensions for the identified file type
-F, --separatorUse a custom string as a separator between filename and file type in the output
-f, --files-fromRead file names to be examined from the specified file, one per line
-h, --no-dereferenceDo not follow symbolic links (affect the symlink itself)
-i, --mimeOutput MIME type strings instead of human-readable file types
--mime-typeOutput only the MIME type of the file
--mime-encodingOutput only the MIME encoding of the file
-k, --keep-goingContinue checking for additional matches after the first match
-l, --listList all patterns and their strength sorted by matching strength
-L, --dereferenceFollow symbolic links to the actual file
-m, --magic-fileSpecify an alternate file or directory containing magic definitions
-N, --no-padDo not pad filenames to align them in the output
-n, --no-bufferFlush stdout after checking each file for real-time output
-p, --preserve-datePreserve the access time of files during analysis
-P, --parameterSet various limits for file processing parameters, like maximum bytes to read, recursion limits
-r, --rawDo not translate unprintable characters to octal representation
-s, --special-filesAlso read and determine the type of special files (block, character devices, pipes, etc.)
-S, --no-sandboxDisable sandboxing to allow the execution of external decompressing programs
-v, --versionDisplay version information and exit
-z, --uncompressAttempt to look inside compressed files
-Z, --uncompress-noreportLook inside compressed files but report only the contents, not the compression method
-0, --print0Output a null character (\0) after each filename for compatibility with tools like cut
--helpDisplay 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




Linux Playground

Scroll to Top