Recover Deleted Files Using Mac Terminal

How to Recover Deleted Files Using Mac Terminal

Last updated:

No robots used, our articles are crafted by humans under strict Editorial Guidelines.

Written by Joel Lockard Joel Lockard Contributing Writer • 7 articles Joel Lockard, a contributing writer for Handy Recovery, primarily showcases his expertise through articles on Apple-related technology and Mac applications, including data recovery. This expertise is rooted in his experience at MacUpdate, where he served as Editorial Content Editor, App Data Editor, and Support Agent for over three years. Since joining our project in 2020, Joel has continued to inspire us with his insightful and innovative content. LinkedIn Approved by Andrey Vasilyev Andrey Vasilyev Editor Andrey Vasilyev is an Editorial Advisor for Handy Recovery. Andrey is a software engineer expert with extensive expertise in data recovery, computer forensics, and data litigation. Andrey brings over 12 years of experience in software development, database administration, and hardware repair to the team. LinkedIn

With how much we use our Macs, we’re constantly adding new data to them but at the same time, deleting things that we don’t need anymore. Sometimes we may delete something only to then realize that we weren’t finished with it and that we wished we would have kept it a little bit longer.

🗨️

While there are multiple ways to recover data on your Mac, in this article we’re going to take a look at how to recover deleted files on Mac using the Terminal. If you would like to use something with a graphical interface, we’re going to take a look at an option that will allow you to do it that way as well.

What Is the Mac Terminal and How Does It Work?

The Terminal provides text-based access to the operating system, in contrast to the mostly graphical nature of the user experience of macOS.

With this text-based interface, you can access shells or other aspects of the operating system at a deeper level and it will allow you to do some tasks that can’t be done with the normal graphical interface.

Now that we know what the Terminal is, let’s talk about how to launch and use it.

How to Launch Terminal on Mac

Launching the Terminal is actually quite easy and it comes built-in as part of the operating system on macOS so you don’t need to download anything to open the app.

  1. Launch Finder and go to Utilities.utilities-folder-in-finder
  2. From within Utilities, look for the Terminal app and double-click on it to launch it.launching the terminal from within finder
  3. You will then be greeted by a new blank Terminal window where we can type commands into the Terminal.blank terminal window

There are various ways to recover files using the Terminal and we’re going to take a look at some below.

How to Use the Terminal to Recover Deleted Files From the Trash

The Trash is where deleted files go. Files are either put into the trash or you can delete them permanently right away.

⚠️

You will need to change your privacy settings so that the Terminal can have Full Disk Access before the steps below will work. Don’t worry though, this is super easy to do and will only take a few steps.

  1. Launch your System Preferences and then navigate to the Security & Privacy tab.
  2. From within here, you will want to look for Full Disk Access and allow the Terminal full access to your Mac.system preferences terminal
  3. Now we can Launch the Terminal. This can be done by going to the Finder and then your Utilities folder. You can learn how to launch the Terminal by reading the steps above.
  4. Now that you’re greeted with a new Terminal window, we can start recovering deleted files.
  5. The first thing that we want to do is navigate to the trash folder so that we can view its contents. Type in the code below and then hit return:
    cd .Trash
  6. Next, we want to bring up the contents of the trash which can be done by typing in the code below and then hitting return.
    ls -al ~/.Trash

    viewing the trash in the terminal

  7. We can recover deleted files by typing in the following with the correct path and then hitting return:
    mv filename ../

    I went skiing recently and deleted a picture so what I can do to recover it is type in mv and then I went to the Trash and dragged the file into the Terminal as this will type out the pathname for me. I have posted the file name below to give you an idea of how it will look. After you have typed out the file name, click return on your keyboard.

    /Users/joellockard/Library/Mobile\Documents/com\~apple\~CloudDocs/.Trash/brighton-sunset-mountain.jpeg ../
  8. After the file has been recovered, go to your home directory folder to view the recovered file.

Recovering Deleted Files From External Devices Trash

External devices such as thumb drives have their own local trash installed on them. These files are hidden by default so that users don’t make changes to them. We can look into them though to check for deleted files. I go into more depth on this in an article where we talk about external hard drive recovery.

  1. Press the keys SHIFT + CMD + . to enable hidden files. You can also enable hidden files and folders by launching the Terminal and typing in:
    defaults write com.apple.finder AppleShowAllFiles TRUE

    and then type in:

    killall Finder

    Now we will be able to see the folder below:

    .Trash
  2. Now that you have run these commands, you should be able to view the contents of the trash folder. Simply browse the folder for any files that you would like to recover.

Now let’s take a look at how to run Time Machine from the Terminal. Time Machine allows you to backup your Mac and recovers data from it which is something that I would highly recommend that you do.

Running Time Machine via the Terminal

Time Machine is a built-in backup utility that allows you to back up your Mac to an external hard drive. Most of the time, you can use the Time Machine interface instead of the Terminal for recovering deleted files.

The Terminal-based method can come in handy if you’re trying to control Macs that are remote or if you’re running scripts on them. You could also use this recovery method to learn how to run Time Machine without using the graphical interface.

Most of the Time Machine commands will have the following in their name:

tmutil
  1. Launch the Terminal. Make sure that the Terminal has full disk access which is explained in the section above.
  2. Type in the following to list all of the time machine backups that you have available:
    tmutil listbackups

    time machine backups in the terminal

  3. Type in:
    tmutil restore ‘$BACKUP_PATH/$ORIGINAL_PATH’ ‘$DESTINATION’

    This will allow you to restore a file from your Time Machine backup. Remember that you will need the entire path when you put in the name of the file.

If all of those Terminal and text-based are getting you excited, there are entire data recovery tools that run within the Terminal.

Command-Line Recovery Software Option for Mac

PhotoRec is a command-line tool that allows you to recover deleted files and I would highly recommend it. While there is a lot to go into when it comes to talking about PhotoRec, I would recommend taking a look at a review and walkthrough that I did of it in the past. You can check out the review here.

selecting media in photorec

You can read more about PhotoRec here and can download it by clicking here.

Are There Any Data Recovery Software Alternatives With a Graphical User Interface?

After learning about the Terminal and PhotoRec, you may decide that you still want to use a recovery option that includes a graphical user interface. The good news is that Disk Drill is a great option when it comes to data recovery.

Disk Drill can recover deleted files on your Mac and other devices. It does this by scanning your hard drive for data that is not visible to you but is still located on your hard drive.[/sc]

Using Disk Drill just requires you to download and install the software and then from there, you can scan your machine and start recovering deleted files.

  1. Download and install Disk Drill.
  2. Select the storage device that you would like to scan. Disk Drill can also recover data from external hard drives as well.selecting external hard drive in disk drill
  3. Wait for the scan process to complete.scan in progress
  4. Browse through the contents and checkmark the items that you would like to recover. You will then want to click the blue Recover button in the lower right-hand corner of the screen.recovering data from within disk drill
  5. After clicking on Recover, you will then have to choose a recovery destination for the files to be saved to.

Conclusion

The Terminal can seem quite scary when using it for the first time but as you can see, it’s not that hard to use and most things only require a few lines of text to be typed in before you can recover deleted files.

I always love knowing more than one way to do something as sometimes one way may make more sense than the other. Being more comfortable with the Terminal will make you an overall better Mac user and even if you don’t want to use it, you can always go back to using the graphical interface. Either way, you learned something new.

First, you need to launch the Terminal and then type in cd .Trash and hit return. Then, you need to type in ls -al ~/.Trash and hit return. After that, type in mv filename ../ with “filename” being the entire path of the file that you’re wanting to recover and hit return.

It is very hard if not impossible to recover deleted items from the Trash using the Terminal. I would recommend using a data recovery tool such as Disk Drill.

The rm command is used to remove files from your Mac and won’t put them back onto your machine. This tool is very powerful and can be dangerous as it deletes data right away without asking you first to confirm the deletion.

About article
Contributing Writer Joel Lockard

This article was written by Joel Lockard, a Contributing Writer at Handy Recovery. It was also verified for technical accuracy by Andrey Vasilyev, our editorial advisor.

Curious about our content creation process? Take a look at our Editor Guidelines.

How do you rate the article? Submitted: