Recover Files Deleted by Command Line on Mac
In this article
- Why Command-Line Deletion Bypasses the Trash
- Immediate Steps to Take Before Attempting Recovery
- Built-in Options: Time Machine and Local Snapshots
- Using Terminal Commands to Recover (Advanced)
- Third-Party Recovery Software: How to Choose and Use
- Common Problems and Misconceptions
- When Recovery Is Impossible and How to Prevent Future Loss
- Frequently Asked Questions
If you deleted files using the command line on a Mac (e.g., with rm), they are not in the Trash and cannot be restored with a simple drag-and-drop. However, recovery is often possible if you act quickly and use the right tools—this article explains your options, the risks, and step-by-step methods to maximize your chances.
Why Command-Line Deletion Bypasses the Trash
When you delete a file in Finder, macOS moves it to the Trash, where it stays until you empty it. The rm command, however, unlinks the file directly from the filesystem, skipping the Trash entirely. This means the file’s data blocks are marked as free space, but they are not immediately overwritten. Until new data is written to those blocks, the original content may still exist on the disk, making recovery possible with specialized software.
Immediate Steps to Take Before Attempting Recovery
The most critical factor in recovery is avoiding any writes to the disk. Follow these steps immediately:
- Stop using the Mac—do not save files, install software, or browse the web (which writes cache files).
- Do not restart if possible, as shutdown processes may write to disk.
- Unmount any external drive that contained the deleted files if it is connected.
- Use a different disk for recovery software—download the tool on another computer or use a USB drive.
Every moment you continue using the Mac increases the chance that new data overwrites the deleted file’s sectors.
Built-in Options: Time Machine and Local Snapshots
If you have Time Machine enabled, you may be able to restore the file from a backup. Navigate to the folder where the file was, open Time Machine (from the menu bar or System Settings), and browse to a date before deletion. If you don’t have a Time Machine backup, macOS may still have local snapshots (APFS only) if Time Machine is on but no backup drive is connected. Check with tmutil listlocalsnapshots / in Terminal—if snapshots exist, you can mount them with mount_apfs or use Time Machine’s “Browse Other Time Machines” option.
Using Terminal Commands to Recover (Advanced)
For users comfortable with the command line, tools like fsck or diskutil are not designed for file recovery. However, you can try to recover from an APFS snapshot directly:
- List snapshots:
tmutil listlocalsnapshots / - Mount a snapshot:
mkdir /tmp/snap && mount_apfs -o ro -s com.apple.TimeMachine./ /tmp/snap - Copy the file back:
cp /tmp/snap/path/to/file /original/location
This works only if a snapshot exists from before the deletion. For most users, third-party software is more practical.
Third-Party Recovery Software: How to Choose and Use
Several reputable recovery tools support macOS, including Disk Drill, PhotoRec, and TestDisk (both free and open-source). When choosing software, consider:
- File system support—APFS is standard on modern Macs, but older HFS+ is also common.
- Recovery mode—some tools offer a “quick scan” for recently deleted files and a “deep scan” for formatted drives.
- Preview capability—look for tools that let you preview files before paying for recovery.
To use PhotoRec (command-line based):
- Download and extract PhotoRec on another machine or a USB drive.
- Boot the Mac from a recovery drive or run it from the USB (if you can boot from it).
- Run
photorecand select the disk (not the partition) for the best chance. - Choose where to save recovered files—use an external drive.
Note: PhotoRec recovers files by their signature, so filenames are lost; you’ll get a folder of files with generic names.
Common Problems and Misconceptions
Many users believe that emptying the Trash makes files unrecoverable—this is false; the data remains until overwritten. Another misconception is that Secure Empty Trash (removed in newer macOS) guarantees deletion—it doesn’t fully protect against forensic recovery. The biggest practical problem is that people wait too long or keep using the Mac, causing overwrites. Also, if you deleted the file from an SSD, TRIM may have already erased the blocks, making recovery impossible. SSDs automatically TRIM deleted blocks, so the chance of recovery is much lower than on a traditional hard drive.
When Recovery Is Impossible and How to Prevent Future Loss
If the file was on an SSD and you continued using the Mac for even a few minutes, TRIM likely wiped the data. Also, if you’ve done a full disk encryption (FileVault) and the key was changed, recovery is impossible. To avoid this situation in the future:
- Enable Time Machine and back up regularly.
- Use
trashcommand instead ofrm—install a tool liketrash(via Homebrew) that moves files to the Trash. - Be extra careful with
rm -rf—always double-check the path. - Consider using version control (e.g., Git) for important files.
Frequently Asked Questions
Can I recover a file deleted with rm if I don't have a backup?
Yes, but only if the data blocks haven’t been overwritten. On a hard drive, you have a decent chance if you stop using the Mac immediately and use recovery software. On an SSD, the chance is low due to TRIM.
Is there a way to undo rm in Terminal?
No, rm does not have an undo feature. The only way to “undo” is to restore from a backup or recover with third-party tools.
Does Time Machine back up files deleted from the command line?
Yes, Time Machine backs up all files regardless of how they were deleted, as long as they existed at the time of the last backup. You can restore them from the backup.
What is the best free recovery tool for Mac?
PhotoRec and TestDisk are free and open-source, but they are command-line based and recover files without original names. Disk Drill has a free trial that allows previews, but full recovery requires a paid license.
How long do I have to recover a deleted file?
There is no fixed time. On a hard drive, it could be days or weeks if you don’t write much data. On an SSD, it could be seconds or minutes due to TRIM. Always act immediately.