You can get a file deleted with Unix rm back only if its data still exists somewhere: in a backup, in a copy another process still holds open, on a snapshot-enabled filesystem, or — as a last resort — as raw blocks on the disk that haven't been overwritten yet. Plain rm unlinks the directory entry and frees the inode; it does not "move" the file to a trash folder, so there's no undelete command to run. The method that works depends entirely on which of those four conditions is true, and on how quickly you stop writing to the affected volume.

What rm actually does to your file, in one paragraph of mechanism

Understanding the deletion model tells you why recovery is conditional. A Unix filesystem stores a file in two pieces: the directory entry (a name mapped to an inode number) and the inode (metadata plus pointers to data blocks). rm removes the directory entry and decrements the inode's link count. When the link count reaches zero and no process has the file open, the inode and its data blocks are marked free in the filesystem's allocation bitmap. The bytes may sit untouched for minutes or months — but the filesystem is now free to reuse them. Every write to that partition raises the chance of overwrite. That's the boundary: recovery works when the data hasn't been reused, and it fails silently when it has.

First, check whether "deleted" is what actually happened

Before any recovery attempt, rule out the cheaper explanations. Plenty of "deleted" files were never unlinked — they were renamed, moved, shadowed by an overlay mount, or written to a different directory than the shell's current one.

Run these checks in order:

1: Confirm you're in the directory you think you're in. Run pwd and ls -la to see hidden files (names starting with .), since a leading dot hides a file from plain ls. 2: Search by name across the tree. Use find /path/to/search -name 'filename*' 2>/dev/null to catch renamed or relocated copies. 3: Rule out the typo case. If you ran rm file in the wrong directory, the file may still exist in the right one. cd there and look. 4: Check for a wrapper. Aliases or shell functions sometimes redirect rm; run type rm and alias to see whether your "rm" is really rm -i or a custom script. 5: Check the trash convention on your desktop. Desktop environments (GNOME, KDE, XFCE) move files to ~/.local/share/Trash/files/ rather than unlinking them. If the deletion came from a file manager rather than a shell rm, look there first.

If all five come up empty, treat it as a genuine unlink and move to the next section. The clock is now running on every write to that filesystem.

Stop writing to the filesystem immediately

This is the single most important step, and it's the one people skip. Every cp, every log write, every browser cache flush, and every package update writes to the disk and can reclaim the freed blocks your file's data occupies.

What to do:

Unmount if you can. umount /mountpoint (or umount /dev/sdXn). If it's a data disk and not the root filesystem, this is straightforward and safely removes all overwrite risk. If it's the root filesystem, you can't unmount it. Remount read-only as root: mount -o remount,ro /. Some processes will complain, and you may need to stop services first; a live USB boot is the cleaner alternative if it's available. Redirect logs elsewhere if you can't remount. A runaway log file is the classic silent overwriter of deleted file blocks. If the disk is failing or the file is valuable, stop and make a full block-level image with dd or ddrescue before anything else, then do recovery against the image, never the original.

For a filesystem that's still mounted read-write on a busy server, expect the odds to drop fast. On an idle laptop where you deleted one small text file and then sat still, the odds stay good for hours.

Recover using an open file descriptor (the best-kept trick)

If any process still has the file open when it was unlinked, the inode survives. This is the most reliable recovery path on a running system, and it costs nothing.

Find processes holding deleted files: lsof +L1 lists open files with a link count of zero. Filter for your file with lsof +L1 | grep filename. If you find one, recover via /proc//fd/ where N is the file descriptor number from lsof: cp /proc/1234/fd/7 /tmp/recovered-file. This copies the still-open inode's contents. Alternatively, cat /proc/1234/fd/7 > /tmp/recovered-file does the same thing when cp refuses.

The success check: compare size and content hash against what you expected (sha256sum on a known-good copy if you have one). The failure case: lsof returns nothing, meaning no process holds it and this route is closed.

The classic gotcha: a file deleted while still open by a process (a log file, a database WAL, a running app's config) keeps consuming disk until the process closes it. That's why df stays full after a rm on a log until the logging process is restarted. The corollary is good news for recovery — the data is still fully present under /proc.

Restore from backup — check all four backup layers

Most production data has more than one backup layer, and people check only one. Work through these in order:

Backup layer Typical location or tool Check
Filesystem snapshots LVM lvcreate --snapshot, ZFS zfs snapshot, btrfs btrfs subvolume snapshot, or filesystem-native snapshot tools lvs on LVM, zfs list -t snapshot, btrfs subvolume list
Scheduled system backups rsync targets, tar archives, restic/borg/duplicity repositories The tool's own list/snapshots subcommand
Cloud or off-site Provider backup consoles (AWS EBS snapshots, GCP persistent disk snapshots, Azure disk snapshots) The provider's backup dashboard
Application-level Database dumps, editor swap/backup files, version control history .swp files, git log, app-specific backup folders

A detail worth knowing: on LVM, a snapshot taken after the deletion already reflects the deletion. You need the previous snapshot. On ZFS or btrfs, snapshots are cheap and frequent, so check the newest one preceding the deletion. If your root filesystem is on btrfs or ZFS and you have snapshots, mounting the snapshot read-only and copying the file out is a one-line fix — cp /mnt/snapshot/path/to/file /path/to/file.

Success check: the restored file's size, modification time, and content look right. A timestamp that predates your last edit means you restored an older version — often acceptable, sometimes not, and worth flagging.

Editor swap files and temporary copies

Text editors and applications often leave a recoverable copy without telling you. This is worth ten minutes before you reach for serious recovery tools.

Vim: look for .filename.swp in the same directory, or in /.vim/swap/ depending on your directory setting. Run vim -r filename to recover. Emacs: check #filename# (auto-save) and filename (backup file). M-x recover-file works on the auto-save. LibreOffice and many GUI editors: look in ~/.config/libreoffice/ or the app's recent-files and backup directories. Version control: if the file was ever committed, git log --all -- path/to/file and git show :path/to/file recover it. A file that was never added to git is not in git, so check git status memory carefully.

~/.cache and /tmp are also worth a look for generated temporary copies, though /tmp cleans itself on many distributions.

Recover with extundelete or ext4magic (ext3/ext4 only)

These tools scan the filesystem for recently freed inodes and reconstructed data. They only work on ext3/ext4, and only when the filesystem is unmounted or mounted read-only. Before installing anything, remember you're writing to the system — install from a live USB or pre-install the tools.

Steps for a typical ext4 recovery:

1: Unmount the filesystem or remount it read-only. 2: Identify the partition device: lsblk. 3: Install the tool on a live system or separately, then run extundelete in read-only mode: extundelete --restore-file path/to/file /dev/sdXn. 4: Omit --restore-file to restore all recoverable files, or use --restore-all (like it sounds — dump everything recoverable into ./RECOVERED_FILES/). 5: Check the output directory and look at file sizes and timestamps.

Success check: the recovered file opens and matches expected content. Failure handling: extundelete can return empty results when the inode was reused or the filesystem is mounted read-write. In that case, stop and use photorec. Note that extundelete may report "No deleted inodes found" on filesystems with journaling or extents that don't preserve the needed information; that's a limitation of the filesystem metadata, not a mistake on your part.

A caveat about extundelete's currency: its upstream project hasn't had new releases for a while (last commonly cited version 0.2.4). That's fine for a stable tool against stable filesystem versions, but on very new kernels or unusual ext4 features, check first that it can read your filesystem cleanly. If it reports errors, stop rather than forcing it.

Raw block scanning with PhotoRec or TestDisk (any filesystem)

When filesystem-level reconstruction fails, PhotoRec scans raw blocks for file signatures and reconstructs files without metadata. It works across ext, XFS, btrfs, NTFS, FAT, and more. TestDisk, its sibling, can sometimes rebuild directories from raw structures.

Steps:

1: Confirm the target partition's device name with lsblk. 2: Run photorec /dev/sdXn (do not pass a mounted writeable path to the target). 3: Choose the filesystem type (or "Other" if unsure) and the file types to search for. 4: Choose an output directory on a different disk, with enough free space. 5: Let it scan; it writes recovered files under numbered recup_dir.* folders.

Success check: open a few recovered files and confirm they're not truncated. Failure modes: PhotoRec cannot recover files with a fragmented layout reliably, and it loses original filenames (you get f1234567.pdf-style names) unless TestDisk accompanies it. Recovered files are often partial, so expect to triage.

For filesystems other than ext3/ext4, this is the main path. XFS, in particular, has essentially no undelete tooling; raw scanning is the practical fallback.

Filesystem snapshots and copy-on-write filesystems — check before anything else

If your data lives on ZFS or btrfs with snapshots configured, the deletion is a non-event. You recover from the previous snapshot without touching the disk.

ZFS: zfs list -t snapshot shows snapshots. Create a read-only clone or use zfs clone, or simply access a snapshot under .zfs/snapshot// if the visibility property is set. Then cp the file out. btrfs: btrfs subvolume list and btrfs subvolume snapshot; snapshots are usually mounted under a snapshots directory on distributions that enable this (check /etc/fstab for the subvolume layout). LVM snapshots: lvs lists snapshots; mount the previous snapshot read-only under a temporary path and copy. Apple APFS or Windows ReFS: these are outside Unix proper but expose snapshot tools (tmutil on macOS for Time Machine local snapshots). On macOS, local snapshots are often available for recent deletions.

This section deserves priority over any recovery software. If you're on a copy-on-write filesystem with snapshots, your recovery is complete before you open a terminal. On most default Linux desktops (ext4 on LVM without snapshots), you're out of luck on this route.

What to do when none of the above works

If you've verified no process holds the file, found no snapshot or backup, and raw scanning yields nothing usable, consider your realistic options:

Data recovery services can attempt platter-level recovery on a failing disk, but not to inevitable success, and prices vary by provider and case. Ask for a diagnostic first; some services charge a flat fee and return "no recovery, no fee." Stop writing to the disk while you decide. Every hour of use reduces viability. Check whether a copy exists elsewhere on the network. A coworker's machine, a CI cache, or a shared mount may still have it.

A concrete failure case worth planning for: if the deleted file was large and the disk was nearly full, the freed blocks were likely allocated quickly by the next write. That's the worst case, and it's why "stop writing" is the first instruction rather than a footnote.

Preventing the next loss

The recovery methods above all come down to conditions that are much easier to create in advance than to satisfy after the fact. A few cheap habits:

Use rm -i for interactive confirmation, or a trash-cli wrapper (trash-put) that moves files to a recoverable location. Keep short-interval snapshots on any copy-on-write filesystem you can. ZFS and btrfs snapshots cost almost nothing in space for files that don't change. Run lsof +L1 before deleting an in-use file to see whether a process holds a reference. For long-running services, rotate and archive logs rather than deleting them live — the disk-space paradox above is a symptom worth knowing. Back up to a destination on a different physical disk and check that the backup job actually restored a test file once, not just that it ran.

What rm cannot do, and what to verify before relying on any tool

rm has no option that makes a file recoverable. There's no rm --recover, no undelete flag, and no hidden trash inside coreutils. Any page promising one is describing a wrapper tool like trash-cli or a desktop trash folder, not rm itself. Before you install recovery software, confirm the filesystem type with lsblk -f or df -T — tools are filesystem-specific, and using extundelete on XFS wastes time. And if the disk shows I/O errors or uncorrectable sectors, stop and image the disk before attempting anything that writes.

FAQ

Does rm -f behave differently for recovery than rm?

rm -f suppresses errors and confirmation but still unlinks the file the same way. The recovery situation is identical; only the risk of deleting a wrong file silently goes up.

Can I recover a file deleted with rm -rf on a directory?

The mechanism is the same per file. rm -rf doesn't add any extra deletion depth; the directory entries are removed recursively. Recovery odds depend on what has reused the blocks since, not on the -r flag.

Is extundelete safe to run on a mounted filesystem?

Not reliably. Running it against a read-write mount risks the tool writing to the filesystem and accelerating loss of the data you're trying to recover. Unmount or remount read-only first.

What if the deleted file is on the root partition of a live server?

Remount read-only (mount -o remount,ro /) if you can tolerate the service interruption, or boot from live media. If neither is possible, expect lower odds, prioritize any backup or snapshot route, and avoid installing packages or writing logs to that volume.