Operations 4 min read

Master lsof: Inspect Open Files, Find Processes, and Recover Deleted Data

This guide introduces the lsof utility, explains its basic usage for listing open files, demonstrates practical scenarios such as identifying files used by specific processes, checking which process occupies a port, and leveraging lsof to recover deleted files, helping administrators troubleshoot I/O and security issues.

Java High-Performance Architecture
Java High-Performance Architecture
Java High-Performance Architecture
Master lsof: Inspect Open Files, Find Processes, and Recover Deleted Data

lsof Overview

lsof (list open files) is a tool that lists all open files on a system. In Linux, everything is a file, so opened files can be regular files, directories, network file system files, character devices, pipes, sockets, etc.

To see which files are currently open and obtain related information, use the lsof command.

Basic Usage

View files opened by a process

Example: list files used by MySQL # lsof -c mysql Find the process using a specific file

Example: see which process is accessing the system log file

# lsof /var/log/messages

Practical Cases

(1) Show files accessed by a given PID # lsof -p PID This is useful when the system experiences high I/O load; tools like top or iotop can identify the offending processes, and lsof reveals which files those processes are using to diagnose the issue.

(2) Identify which process is using a specific port

Example command: # lsof -i:PORT_NUMBER This helps determine whether an unknown port is being used, which can indicate a security problem.

(3) Recover deleted files

In Linux, deleted files remain accessible if a process still holds them open. To recover a deleted log file, first confirm a process is using it, then copy the file descriptor.

Check which process is using the file: # lsof | grep message Rewrite the file (replace PORT_NUMBER with the appropriate descriptor):

# cat /proc/PORT_NUMBER/fd/2 > /var/log/messages
Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

LinuxSystem Administrationprocess monitoringlsofFile Recoveryopen files
Java High-Performance Architecture
Written by

Java High-Performance Architecture

Sharing Java development articles and resources, including SSM architecture and the Spring ecosystem (Spring Boot, Spring Cloud, MyBatis, Dubbo, Docker), Zookeeper, Redis, architecture design, microservices, message queues, Git, etc.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.