file command
Command: file
π Purpose
The file command is used to identify the type of a file.
It examines the file’s contents, not its extension, and tells you what kind of data it holds.
This helps you distinguish between:
-
Text files
-
Executable files
-
Object files
-
Scripts
-
Images, PDFs, etc.
π§© Basic Syntax
Example:
⚙️ How It Works
The file command uses a three-step test to determine a file’s type:
-
Filesystem test – Checks the type from filesystem information (e.g., directory, symbolic link, block device).
-
Magic test – Reads the first few bytes of the file (called the magic number) and compares them with entries in
/usr/share/misc/magic.mgc. -
Language test – If it’s a text file,
filechecks whether it contains source code (like C, Python, shell script, etc.).
Example 1: Identifying Common Files
Let’s try on different files.
Output:
/bin/ls: ELF 64-bit LSB executable, x86-64, dynamically linked, interpreter /lib64/ld-linux-x86-64.so.2π§© Explanation:
-
ELF → Executable and Linkable Format (used for binaries in Linux)
-
64-bit → Architecture type
-
Dynamically linked → Uses shared libraries
-
Interpreter → Loader that runs the binary
Output:
Output:
Output:
Output:
Example 2: Using Wildcards
You can test multiple files at once:
Output:
Example 3: Checking a Directory or Link
Output:
Output:
Example 4: Check Character Encoding
Output:
This helps detect encoding issues (ASCII, UTF-8, ISO-8859, etc.).
Example 5: Checking Object or Library Files
Output:
π§© Explanation:
-
Relocatable → Object file (not yet linked)
-
with debug_info → Contains debugging symbols (compiled with
-g)
Example 6: Checking Compressed Files
Output:
Example 7: Detect Script Type
Output:
π Commonly Used Options
| Option | Description |
|---|---|
-b | Brief output (omit filename) |
-i | Print MIME type instead of description |
-z | Examine compressed files |
-L | Follow symbolic links |
-s | Read block/character special files |
--mime-type | Show MIME type (e.g., text/plain) |
--mime-encoding | Show encoding (e.g., utf-8) |
Examples
Output:
Output:
Output:
Lab Exercise
Aim:
To study the use of the file command to identify the type and properties of files in Linux.
Procedure:
-
Create different types of files:
-
Execute the following commands:
-
Record and compare outputs.
Sample Observation Table
| Filename | Command Used | Output |
|---|---|---|
| text.txt | file text.txt | ASCII text |
| script.sh | file script.sh | Bash script |
| hello.c | file hello.c | C source text |
| hello | file hello | ELF executable |
| /bin/ls | file /bin/ls | ELF 64-bit binary |
Learning Outcomes
Students will:
-
Understand how Linux identifies files based on content, not extensions.
-
Learn about ELF executables, text encoding, and symbolic links.
-
Develop a foundational understanding of how the OS classifies and manages files.
Summary
| Command | Purpose |
|---|---|
file filename | Identify file type |
file -b filename | Show type only |
file -i filename | Show MIME type |
file * | Identify all files in directory |
file --mime-type file | Display MIME type |
file --mime-encoding file | Display text encoding |
Comments
Post a Comment