The file command in Unix and Linux is used to determine the type of a file by examining its contents, rather than relying on the filename extension alone. It's a handy utility for identifying the format of a file, especially when dealing with unknown or ambiguous file types.
Basic Usage
The basic syntax for the file command is:
file [options] [file(s)]
options: Optional command-line options to modify the behavior of file.
file(s): The name(s) of the file(s) to examine.
Examples
Determine File Type
To determine the type of a file:
file filename.txt
This command examines filename.txt and prints out a description of its contents, such as ASCII text, HTML document, JPEG image, etc.
Handling Multiple Files
To check the types of multiple files:
file file1.txt file2.jpg file3.pdf
This command checks the types of file1.txt, file2.jpg, and file3.pdf and displays their respective file types.
Options
Verbose Output
-v: Verbose mode, providing additional details about the file.
Force Check
-f: Force file check, even if the file does not exist.
Mime Type
--mime-type: Display MIME type (Multipurpose Internet Mail Extensions) of the file.
Practical Use Cases
Scripting and Automation
In scripts, the file command can be used to automate decisions based on file types. For example:
#!/bin/bash
for f in *.dat; do
file "$f" | grep -q "ASCII text" && echo "$f is a text file"
done
This script checks all .dat files in the current directory and prints a message if they are identified as ASCII text files.
File System Management
When managing files with unknown extensions or when dealing with diverse file types, the file command helps in quickly identifying the nature of each file.
Summary
The file command is a valuable tool in Unix and Linux for identifying file types based on their contents. It's useful in scripting, file system management, and when handling a variety of files with ambiguous extensions. Understanding its usage and options can streamline file handling tasks and improve efficiency.
help
Usage: file [OPTION...] [FILE...]
Determine type of FILEs.
--help display this help and exit
-v, --version output version information and exit
-m, --magic-file LIST use LIST as a colon-separated list of magic
number files
-z, --uncompress try to look inside compressed files
-Z, --uncompress-noreport only print the contents of compressed files
-b, --brief do not prepend filenames to output lines
-c, --checking-printout print the parsed form of the magic file, use in
conjunction with -m to debug a new magic file
before installing it
-e, --exclude TEST exclude TEST from the list of test to be
performed for file. Valid tests are:
apptype, ascii, cdf, compress, csv, elf,
encoding, soft, tar, json, text,
tokens
--exclude-quiet TEST like exclude, but ignore unknown tests
-f, --files-from FILE read the filenames to be examined from FILE
-F, --separator STRING use string as separator instead of `:'
-i, --mime output MIME type strings (--mime-type and
--mime-encoding)
--apple output the Apple CREATOR/TYPE
--extension output a slash-separated list of extensions
--mime-type output the MIME type
--mime-encoding output the MIME encoding
-k, --keep-going don't stop at the first match
-l, --list list magic strength
-L, --dereference follow symlinks (default if POSIXLY_CORRECT is set)
-h, --no-dereference don't follow symlinks (default if POSIXLY_CORRECT is not set) (default)
-n, --no-buffer do not buffer output
-N, --no-pad do not pad output
-0, --print0 terminate filenames with ASCII NUL
-p, --preserve-date preserve access times on files
-P, --parameter set file engine parameter limits
bytes 1048576 max bytes to look inside file
elf_notes 256 max ELF notes processed
elf_phnum 2048 max ELF prog sections processed
elf_shnum 32768 max ELF sections processed
encoding 65536 max bytes to scan for encoding
indir 50 recursion limit for indirection
name 50 use limit for name/use magic
regex 8192 length limit for REGEX searches
-r, --raw don't translate unprintable chars to \ooo
-s, --special-files treat special (block/char devices) files as
ordinary ones
-S, --no-sandbox disable system call sandboxing
-C, --compile compile file specified by -m
-d, --debug print debugging messages