Data Recovery

How to Recover Deleted Files Using Mac Terminal

5 Mins read
Recover Deleted Files Using Mac Terminal

If you lost important files on your Mac, recover them in style using the Terminal app. Terminal is a powerful command line tool for macOS that can quickly execute tasks on your machine (or remote ones). There are multiple ways you can use it to restore data.

But if you don’t use Mac Terminal often, it can be confusing to figure out the right commands. This article provides step-by-step instructions for every method to restore deleted files and recover deleted folders with Mac Terminal. Read on.

Set Up Your Terminal App for Recovery

Before attempting any of the methods below, you first have to grant Full Disk Access to the Terminal app. This allows Terminal to modify user-protected data. If you skip this step, some commands won’t work. Here’s how to do it:

  1. Launch System Preferences > Secruity & Privacy.Launch System Preferences > Secruity & Privacy
  2. Navigate to the Privacy tab and click the lock button to allow changes. Then, select Full Disk Access on the left sidebar. Locate Terminal in list on the right and tick the box beside it.Navigate to the Privacy tab
  3. Click the lock button again and close the System Preferences window.

How to Use Terminal to Recover Deleted Files on Mac

As we talked about earlier, there are different ways to restore data using the Mac Terminal. The method you use depends on what caused the data loss and how you have your Mac set up. Don’t worry – each section clearly describes the situation that the featured method is best suited for.

Option #1: Try to Undelete Files From Trash Using Mac Terminal

If you accidentally deleted your data but you haven’t emptied your Trash folder, you can undelete files or recover deleted folders with the Mac Terminal.

  1. Open Finder > Applications > Utilities and launch Terminal.launch Terminal
  2. Type cd .Trash and hit Return.Type cd .Trash
  3. Type ls -al ~/.Trash and hit Return.Type ls -al ~/.Trash
  4. Type mv yourfile ../ and hit Return. Make sure to replace yourfile with the full name of the file you want to restore.Type mv yourfile
  5. Type quit and hit Return to close the Terminal app. Your file should be restored to its original folder.

Option #2: Use Mac Terminal to Recover Files from Time Machine Backup

Time Machine is a powerful native Mac tool that lets users save incremental backups of their data. While it offers a great GUI (graphical user interface), you can also use Terminal to restore your backups.

However, note that this method requires Time Machine to have been set up before the data loss occured. If don’t have Time Machine activated, try the other methods in this article.

Info: This method also requires Full Disk Access for the Terminal app. You can enable Full Disk Access in the System Preferences > Security and Privacy tab.

  1. Launch the Terminal app (Applications > Utilities).
  2. Type tmutil listbackups and hit return. Take note of the name of the backup you want to restore.
  3. Type tmutil restore ‘/Volumes/Time Machine Backups/Backups.backupdb/yourcomputername/yourbackupfilename/Macintosh HD/Users/yourusername/Documents/yourfile’ ‘~/Documents/yourfile’ and hit return. But first, replace the following elements:

Change yourcomputername to the name of your Mac. Change yourbackupfilename to whichever you chose in step 2. Change yourusername to your own username. Change Documents (in both instances) to the location that originally stored the file you want to recover. Change yourfile to name of the file you want to restore.

Option #3: Get a Terminal-Based Data Recovery App for Mac

If you emptied your Trash folder and you don’t have a Time Machine backup available, you’ll need to use data recovery software… And yes, we’ll still be using Mac Terminal.

TestDisk/PhotoRec are a 2-in-1 data recovery software that can be controlled entirely through Terminal. And great news: TestDisk/PhotoRec are 100% free open-source software (which can explain the lack of an interface).

  1. Download and install TestDisk/PhotoRec.
  2. Launch Terminal (Finder > Applications > Utilities).
  3. Type sudo photorec and hit return on your keyboard. Enter your password if prompted.Type sudo photorec
  4. Use your up and down arrow keys to highlight the drive you want to restore data from. Then, use your left and right arrow keys to highlight Proceed and hit return.arrow keys to highlight Proceed
  5. Use your arrow keys to highlight the partition you want to restore data from. Or you can highlight the Unknown/[Whole disk] entry to scan all patitions. Then, highlight the Search button and hit return. You can also launch the File Opt menu to specify the file types you want to recover.you can highlight the Unknown
  6. Highlight Other and hit return.Highlight Other and hit return
  7. Up your up and down arrow keys to browse folders. Use your left and right arrow keys to go in and out of the highlighted folder. Then, hit C to begin the recovery process.hit C to begin the recovery process

Recover Files Deleted with RM Command on a Mac

RM is a powerful delete command that is super useful for users who want to delete a lot of files efficiently. Fortunately, RM does not overwrite files – however, it completely bypasses the Trash folder. So the only way to recover files deleted with RM command on a Mac is to use data recovery software.

We showed you how to use Terminal-based app PhotoRec in the last section. This time, we’ll be using a tool called Disk Drill. Unlike TestDisk/PhotoRec, Disk Drill has a well-designed interface that makes recovery simple for users at any level.

Note: Disk Drill Basic for Mac does not offer free data recovery. However, you can preview as many files as you want (including videos). There are free data recovery software aside form TestDisk/PhotoRec, but they all have their own limitations. Try at your own risk.

  1. Download and install Disk Drill.
  2. Launch Disk Drill (Finder > Applications).Launch Disk Drill
  3. Select the drive you want to restore (your system drive is usually labeled as “Apple SSD” or similar), then click Search for lost data.click Search for lost data
  4. Wait for the scan to finish, then click Review found items.click Review found items
  5. Use the search bar in the top-right corner or select a file type from the left sidebar to narrow down the results.Use the search bar
  6. To preview your files, hover your mouse pointer to the right of any file and click the eye button that appears.preview your files
  7. Select the files you want to restore using the boxes in the left-most column. Then, click Recover.Select the files you want to restore
  8. Use the folder button and select a location on a drive or partition that is different than the one we just scanned. Then, click OK.select a location on a drive or partition

FAQ

Conclusion

Mac Terminal is powerful and efficient, and it’s a great way to restore your data all from a single window. But you know what’s even better? Backing up your files! We briefly covered Time Machine in one of the sections above – it’s super powerful and easy-to-use, so we highly recommend enabling it on your Mac.

Alejandro Santos
1 posts
About author
Alejandro got started in tech as a part-timer in his uncle’s old repair shop, where he helped fix computers and jailbreak iPhones between writing assignments and school work. When he first got his hands on a MacBook, it was love at first site. Today, he and his trusty MBP M1 are on a mission to help people around the world become better technology users through how-to guides, software reviews, and other content.