How to Recover Accidentally Deleted Linux Files with lsof and extundelete
This guide explains why careless file deletion can cause serious loss, introduces Linux tools like lsof and extundelete for recovering deleted files, and provides step‑by‑step commands for installing, locating open file descriptors, and restoring individual or all deleted data safely.
Preface
Deleting files on any system must be done with caution because accidental removal of important files can cause significant personal or corporate loss. Linux, like Windows, offers tools to recover mistakenly deleted files.
Note that recovery is not guaranteed to be 100 % successful; always back up important data and avoid writing new data to the directory of the deleted file before recovery.
1. lsof
Principle
The lsof command cannot directly restore files, but it lists information about files opened by processes. By examining the /proc filesystem you can recover files that have been deleted but are still held open by a process.
Because /proc reflects in‑memory mappings, a file that has been removed from disk can still be read from memory while a process keeps it open.
Important notes
Run as root, since lsof needs access to kernel memory and various files.
Only files that are deleted but still open by a process can be recovered.
If an entire directory was deleted and none of its files are open, this method cannot recover them.
lsof output fields
COMMAND PID USER FD DEVICE SIZE NODE NAMERecovery steps with lsof
lsof /mntIdentify the PID and file descriptor (FD) of the process that still holds the deleted file, then copy the data:
cd /proc/31284/fd/</code>
<code>cat 4 > /mnt/ferris_train.less2. extundelete
Principle
extundelete reads the journal of an ext3/ext4 partition to attempt recovery of files that have been removed.
Advantages
Compared with ext3grep, extundelete supports both ext3 and ext4, works faster, and has a broader scope.
Official resources
Website: http://extundelete.sourceforge.net
Download: extundelete‑0.2.4.tar.bz2 (latest version released January 2013).
Important notes
After data deletion, unmount the affected disk or partition.
If the root partition is affected, boot into single‑user mode and mount it read‑only to avoid overwriting data.
Overwritten data cannot be recovered.
Recovery may still fail; always keep backups and use rm carefully.
Installation
Dependencies
# CentOS</code>
<code>yum install e2fsprogs-devel e2fsprogs* gcc*</code>
<code># Ubuntu</code>
<code>apt-get install build-essential e2fslibs-devCompile and install
wget http://downloads.sourceforge.net/project/extundelete/extundelete/0.2.4/extundelete-0.2.4.tar.bz2</code>
<code>tar xf extundelete-0.2.4.tar.bz2</code>
<code>cd extundelete-0.2.4</code>
<code>./configure</code>
<code>make</code>
<code>make installRecovery operations with extundelete
Identify the filesystem type of the partition to be recovered: df -Th Unmount the partition to avoid further writes: umount /mnt List deletable files (the last column shows "Deleted"): extundelete /dev/vdb1 --inode 2 Restore a specific directory (empty directories cannot be restored):
extundelete /dev/vdb1 --restore-directory ferrisRestore a single file (small files may fail):
extundelete /dev/vdb1 --restore-file openssh-7.7p1.tar.gzRestore all deleted files without specifying names: extundelete /dev/vdb1 --restore-all After a successful restore, a RECOVERED_FILES directory is created in the current working directory containing the recovered data.
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.
Efficient Ops
This public account is maintained by Xiaotianguo and friends, regularly publishing widely-read original technical articles. We focus on operations transformation and accompany you throughout your operations career, growing together happily.
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.
