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

file [options] filename

Example:

file a.out

⚙️ How It Works

The file command uses a three-step test to determine a file’s type:

  1. Filesystem test – Checks the type from filesystem information (e.g., directory, symbolic link, block device).

  2. 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.

  3. Language test – If it’s a text file, file checks whether it contains source code (like C, Python, shell script, etc.).


Example 1: Identifying Common Files

Let’s try on different files.

file /bin/ls

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


file hello.c

Output:

hello.c: C source, ASCII text

file test.txt

Output:

test.txt: ASCII text

file image.png

Output:

image.png: PNG image data, 800 x 600, 8-bit/color RGBA

file script.sh

Output:

script.sh: Bourne-Again shell script, ASCII text executable

Example 2: Using Wildcards

You can test multiple files at once:

file *

Output:

a.out: ELF 64-bit LSB executable hello.c: C source, ASCII text notes.txt: UTF-8 Unicode text

Example 3: Checking a Directory or Link

file /etc

Output:

/etc: directory
file /bin/sh

Output:

/bin/sh: symbolic link to bash

Example 4: Check Character Encoding

file notes.txt

Output:

notes.txt: UTF-8 Unicode text

This helps detect encoding issues (ASCII, UTF-8, ISO-8859, etc.).


Example 5: Checking Object or Library Files

file sum.o

Output:

sum.o: ELF 64-bit LSB relocatable, x86-64, with debug_info

🧩 Explanation:

  • Relocatable → Object file (not yet linked)

  • with debug_info → Contains debugging symbols (compiled with -g)


Example 6: Checking Compressed Files

file archive.tar.gz

Output:

archive.tar.gz: gzip compressed data, from Unix

Example 7: Detect Script Type

file myscript.py

Output:

myscript.py: Python script, ASCII text executable

πŸ“š Commonly Used Options

OptionDescription
-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

file -b a.out

Output:

ELF 64-bit LSB executable

file --mime-type hello.c

Output:

hello.c: text/x-c

file --mime-encoding notes.txt

Output:

notes.txt: utf-8

Lab Exercise

Aim:

To study the use of the file command to identify the type and properties of files in Linux.

Procedure:

  1. Create different types of files:

    echo "Hello OS Lab" > text.txt cat > script.sh #!/bin/bash echo "Test Script" (Ctrl+D) gcc -o hello hello.c
  2. Execute the following commands:

    file text.txt file script.sh file hello.c file hello file /bin/ls
  3. 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

CommandPurpose
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

Popular posts from this blog

Operating Systems OS Lab PCCSL407 Semester 4 KTU BTech CS 2024 Scheme - Dr Binu V P

Exploring the /proc file system

ps command