Operations 7 min read

Recover Accidentally Deleted Linux Files with lsof and extundelete

This guide explains how to restore mistakenly removed Linux files by first checking if the deleting process still holds the file descriptor using lsof, then copying the open file from /proc, and finally using the extundelete tool when the process has already terminated.

Open Source Linux
Open Source Linux
Open Source Linux
Recover Accidentally Deleted Linux Files with lsof and extundelete

Linux does not provide a visible recycle bin like Windows, so file restoration depends on whether the deleting process is still running.

Case 1: The deleting process is still alive

If a process still has the file open, the file’s inode remains accessible. Use lsof | grep deleted to locate such processes, then copy the file from the process’s file‑descriptor directory.

echo "hello py" > testdelete.py
cat >> testdelete.py
# output: hello delete
rm -f ./testdelete.py
# Verify the file is gone
lsof | grep deleted
# Example output shows a process still holding the file
cp /proc/20464/fd/1 /tmp/recovered_testdelete.py

After copying, the recovered file can be inspected with cat /tmp/recovered_testdelete.py.

Case 2: The process has terminated – use extundelete

When no process holds the deleted file, a third‑party recovery tool such as extundelete is required. Follow these steps:

Stop all activity on the affected partition to avoid inode overwriting (unmount the filesystem or stop services).

(Optional) Create a raw backup with dd if=/dev/vdb1 of=/path/backup.img to protect data.

Install extundelete:

wget http://nchc.dl.sourceforge.net/project/extundelete/extundelete/0.2.4/extundelete-0.2.4.tar.bz2
tar jxvf extundelete-0.2.4.tar.bz2
cd extundelete-0.2.4
./configure
make
make install

Run extundelete to restore files:

# Restore a single file by name
extundelete /dev/vdb1 --restore-file passwd
# Restore an entire directory
extundelete /dev/vdb1 --restore-directory deletetest
# Restore all recoverable files
extundelete /dev/vdb1 --restore-all

Recovered files are placed in the RECOVERED_FILES directory; verify their contents with cat or other tools.

Note that restored inodes may receive new filenames, so rename them as needed. The content of the files remains intact.

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 AdministrationlsofextundeleteFile Recovery
Open Source Linux
Written by

Open Source Linux

Focused on sharing Linux/Unix content, covering fundamentals, system development, network programming, automation/operations, cloud computing, and related professional knowledge.

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.