Recover Deleted Linux Files While Their Processes Remain Open
This guide explains how a file deleted on a Linux system can still be recovered when a running process holds an open file descriptor, detailing the use of tail, lsof, the /proc filesystem, and simple copy commands to restore the data.
Why Deleted Files May Still Be Recoverable
When a program accesses a file, the operating system assigns a file descriptor and keeps the underlying inode alive even after the directory entry is removed with rm. As long as a process holds that descriptor, the file’s data remains reachable through the /proc/<pid>/fd interface.
Scenario Covered
The article focuses on the case where the file has been deleted but the process that opened it is still running, which often explains why disk space is not immediately reclaimed on servers.
Step‑by‑Step Demonstration
Create a test file and write some content.
vim rumenz.txt
123
# save and quit
cat rumenz.txt
123Keep the file open with tail -f so the process stays alive. tail -f rumenz.txt Delete the file from another terminal. rm -f rumenz.txt Identify the process that still holds the deleted file using lsof . Install lsof if necessary ( yum install lsof or apt-get install lsof ).
lsof | grep delete | grep rumenz
# Example output:
# tail 10222 root 3r REG 253,1 4 70911074 /root/test/rumenz.txt (deleted)Inspect the process’s file‑descriptor directory to locate the deleted file.
cd /proc/10222/fd
ls -al
# Output shows a link like:
# 3 -> /root/test/rumenz.txt (deleted)Recover the file by copying the descriptor to a new file.
cp 3 /root/test/rumenz.txt
cat /root/test/rumenz.txt
# Displays the original content "123"Underlying Reason
The OS separates the directory entry from the actual data blocks. Deleting a file removes only the directory entry (the inode link), but any open file descriptor still points to the same inode, allowing I/O operations to continue. By accessing the descriptor via /proc, the file can be duplicated before the descriptor is closed.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Liangxu Linux
Liangxu, a self‑taught IT professional now working as a Linux development engineer at a Fortune 500 multinational, shares extensive Linux knowledge—fundamentals, applications, tools, plus Git, databases, Raspberry Pi, etc. (Reply “Linux” to receive essential resources.)
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
