file

file managementLinux/Unix
The file command is one of the most frequently used commands in Linux/Unix-like operating systems. file Determine file type

Quick Reference

Command Name:

file

Category:

file management

Platform:

Linux/Unix

Basic Usage:

file [options] [arguments]

Common Use Cases

    Syntax

    file [options] [file...]

    Options

    Option Description
    -b, --brief Do not prepend filenames to output lines
    -c, --checking-printout Print the parsed form of the magic file for debugging
    -f, --files-from FILE Read the names of the files to be examined from FILE
    -F, --separator STRING Use STRING as the separator between file name and file result
    -i, --mime Output MIME type strings rather than traditional descriptions
    -k, --keep-going Continue looking for matches after finding a match
    -L, --dereference Follow symbolic links
    -m, --magic-file LIST Use LIST as a colon-separated list of magic files
    -n, --no-buffer Do not buffer output
    -z, --uncompress Try to look inside compressed files

    Examples

    How to Use These Examples

    The examples below show common ways to use the file command. Try them in your terminal to see the results. You can copy any example by clicking on the code block.

    # Basic Examples Basic
    file document.txt
    Determine the type of document.txt.
    file -b document.txt
    Display file type without the filename prefix.
    file -i document.txt
    Show the MIME type of the file. # Advanced Examples Advanced file -z compressed.gz Look inside compressed files. file -L symlink.txt Follow symbolic links. file -f filelist.txt Read the files to examine from filelist.txt. file * Determine the type of all files in the current directory. file --mime-encoding document.txt Show only the character encoding of the file.

    Try It Yourself

    Practice makes perfect! The best way to learn is by trying these examples on your own system with real files.

    Understanding Syntax

    Pay attention to the syntax coloring: commands, options, and file paths are highlighted differently.

    Notes

    The file command is used to determine the type of a file based on its contents rather than its filename extension. Key features of the file command: 1. Content Analysis: file examines the content of each file to determine its type, checking for magic numbers, signatures, and other distinctive patterns. 2. Magic Database: It uses a 'magic' database (/usr/share/file/magic) that contains patterns for recognizing various file types. 3. Wide Range of Detection: file can identify various file types including text files, executable binaries, archives, images, audio files, documents, and many more. 4. MIME Type Support: With the -i option, file can output MIME types which are particularly useful for web applications and scripts that need standardized type information. 5. Symbolic Link Handling: By default, file reports on the symbolic link itself, but with -L it will report on the target of the link. 6. Compressed File Inspection: Using the -z option, file can look inside compressed files to determine the type of the compressed content. 7. Character Encoding Detection: file can identify the character encoding of text files, which is valuable when working with international text. The file command is particularly useful in scripts that need to process files differently based on their type, or when dealing with files that have incorrect or missing filename extensions.

    Related Commands

    These commands are frequently used alongside file or serve similar purposes:

    Use Cases

    Learn By Doing

    The best way to learn Linux commands is by practicing. Try out these examples in your terminal to build muscle memory and understand how the file command works in different scenarios.

    $ file
    View All Commands