The Linux file command is a powerful utility that determines the type of data stored in a system file and presents it in a readable format (MIME type). UNIX filenames often lack explicit file extensions, making this command essential for identifying whether a file is an audio file, spreadsheet, text document, or other format. This guide shows you how to use the file command effectively for VPS management and system administration.
Background
The Linux file command performs three sets of tests to classify files: filesystem tests (checking stat system call returns), magic tests (examining fixed data patterns using a magic number database), and language tests (identifying character sets and languages). This comprehensive analysis allows the command to accurately identify file types even when filenames are misleading or lack extensions.
Prerequisites
- SSH access to your VPS or Linux system
- Basic familiarity with the Linux command line
How to Use the Linux File Command
-
Step 1: Understand the Basic Syntax
The basic syntax for the
filecommand is:file [options] [filename]Where:
fileinitiates the Linux file command[options]allows you to add variables to modify the command behavior[filename]represents the file you want to examine
The system responds with information about the file type in standard output format, including data stored in the file, file size, or version information.
-
Step 2: Check a Single File
To examine a basic file, use the command with just the filename:
file test.txtThe output will indicate the file type, such as "ASCII text file", demonstrating the file's actual format regardless of its extension.
-
Step 3: View Only the File Type (Brief Mode)
To display only the file type without the filename, use the
-boption:file -b devisers.txtThis provides a cleaner output showing just the file classification.
-
Step 4: Check Multiple Files at Once
To analyze multiple files simultaneously, use a wildcard (
*) to examine all files of a certain type in the current directory:file *.txtThe command returns output on separate lines for each file, showing details for all matching files in the current directory.
-
Step 5: View MIME Type Information
To see the MIME type of a file in a machine-readable format, use the
-ioption:file -i devisers.csvThis displays the file type in MIME format (e.g.,
text/plain; charset=us-ascii), useful for programming and automation tasks. -
Step 6: Examine Special Files
To read special files that may not be functioning normally or contain special characters, use the
-soption:file -s txt.tThis command provides information about the size and type of special files, including block devices and other system files.
Common Use Cases
- Verify file types: Check if a file named
devisers.csvis actually a CSV file or perhaps a ZIP file with a misleading extension - Troubleshoot file reading issues: Understand why users might face challenges reading a specific file on the system
- Identify system files: Determine types of folders, sockets, FIFOs (pipes), and block special files
- Batch analysis: Examine all files of a certain type in a directory to ensure consistency