Artistic representation of digital data.

The file command, complete guide and cheatseet

The file command is one of the most useful tools for not only finding files but also searching through their contents. This is a complete guide and cheatsheet for the file command.

Introduction

The file command in Linux is used to determine the type of a given file. It examines the file content, rather than its extension, to classify the file as a text, executable, archive, image, etc. It is a vital tool for debugging, scripting, and ensuring files are handled correctly in various workflows. file works on virtually all file systems supported by Linux and is a go-to utility for system administrators, developers, and users working with mixed or unknown file types.

The file command is a robust utility that simplifies file type identification in Linux. Its versatility makes it indispensable for troubleshooting, scripting, and daily file management tasks. Share this guide with anyone needing a clear reference to maximize their efficiency with this powerful tool.


Syntax and usage

file [options] <file_name>

Tips for Effective Usage

  • Combine with Other Tools: Use file with utilities like find or xargs to process a large number of files automatically.
find . -type f | xargs file
  • Create Reports: Redirect output to a file to generate a report of file types. This is EXTREMELY important if you’re doing filesystem maintenance or working large volumes of data, it’ll help you keep a history of your searches and you can later on parse that history with other tools.
file * &gt; report.txt
  • Identify Corrupt Files: since file also lets you analyze the contents of the data, use file to quickly identify files that may not be what they claim (e.g., mislabeled extensions).

Key Options

  • -b: Outputs just the file type without the file name.
  • -i: Displays the MIME type of the file.
  • -f : Reads filenames from a given file (one per line) and outputs their types.
  • -L: Follows symbolic links and identifies the target file’s type.
  • -s: Examines block or character special files to determine their type.
  • –mime: Similar to -i, but shows both MIME type and encoding.
  • -z: Inspects compressed files.
  • –exclude : Excludes files or directories matching the given pattern.
  • –help: Displays help and exits.
file -b example.txt
file -i image.jpg
file -f filelist.txt
file -L symlink
sudo file -s /dev/sda1
file --mime document.pdf
file -z archive.tar.gz
file --help
file -z archive.tar.gz
file --exclude=*.tmp *



Common Usage Examples

Basic File Type Identification

To determine the type of a single file:

file document.txt

Check Multiple Files

To check the type of multiple files:

file file1 file2 file3

Checking MIME Types

For determining the MIME type of files:

file -i video.mp4

Handling Symbolic Links

To identify the target type of a symbolic link:

file -L symlink_to_binary

Processing a List of Files

Use a file containing a list of filenames:

file -f filelist.txt

Inspecting Compressed Files

To identify the contents of a compressed file:

file -z logs.tar.gz

Inspecting Device Files

For identifying device file types:

sudo file -s /dev/sda1

Leave a Reply

Your email address will not be published. Required fields are marked *