Skip to content

Latest commit

 

History

History
129 lines (86 loc) · 3.09 KB

File metadata and controls

129 lines (86 loc) · 3.09 KB

file

The file command is used to determine the type of a file based on its contents, rather than its extension. It can identify various file types including text files, executable binaries, and more.

Basic Usage

  • Syntax:

    file [options] [file...]
  • Examples:

    file example.txt
    • Displays the file type of example.txt.
    file -i example.txt
    • Displays the MIME type of example.txt.
    file -b example.txt
    • Displays the file type without the filename.
    file -f filelist.txt
    • Reads filenames from filelist.txt and displays their types.

Common Options

  • -b or --brief:

    • Displays only the file type, omitting the filename.
    file -b example.txt
    • Outputs just "ASCII text" instead of "example.txt: ASCII text".
  • -i or --mime:

    • Displays the MIME type of the file.
    file -i example.txt
    • Outputs text/plain; charset=us-ascii for a text file.
  • -f [file] or --files-from=[file]:

    • Reads a list of filenames from the specified file and displays their types.
    file -f filelist.txt
    • Outputs the types of all files listed in filelist.txt.
  • -z or --uncompress:

    • Tries to uncompress files before determining their type.
    file -z compressed_file.gz
    • Identifies the type of a compressed file after decompression.
  • --help:

    • Displays help information about the file command.
    file --help
    • Shows usage information and available options.

Common File Types Identified

File Type Description
ASCII text Plain text file
HTML document File containing HTML markup
JPEG image JPEG image file
gzip compressed Gzip compressed file
ELF 64-bit LSB executable 64-bit Linux executable file
PDF document Portable Document Format file
directory Directory or folder

Quick Tips

  • Determining File Type:

    • Use file to get detailed information about a file's type.
    file example.zip
    • Outputs something like Zip archive data, at least v2.0 to extract.
  • Handling Multiple Files:

    • You can specify multiple files to check their types in a single command.
    file file1.txt file2.png file3.zip
    • Outputs the type for each file listed.
  • Using with Pipes:

    • Use file in combination with other commands to analyze file types of output.
    ls | xargs file
    • Identifies the types of files listed by ls.

Summary

The file command is a useful tool for identifying the type of a file based on its content rather than its extension. It provides a straightforward way to determine whether a file is a text document, an image, an executable, or any other type. By using various options, you can customize the output to fit your needs and handle multiple files efficiently.