X

How to recover items in the OS X trash using Time Machine

If you have stored an item in the OS X trash and then deleted it, recovering it with Time Machine can be a bit difficult. Here is how to do this.

Topher Kessler MacFixIt Editor
Topher, an avid Mac user for the past 15 years, has been a contributing author to MacFixIt since the spring of 2008. One of his passions is troubleshooting Mac problems and making the best use of Macs and Apple hardware at home and in the workplace.
Topher Kessler
4 min read

The trash in OS X is generally used to delete items, but because items can be placed in the trash without being immediately deleted, people commonly end up storing items in the trash. When this happens it may be difficult to remember exactly where the items originated from on disk, so if you empty the trash and then need to recover a document it may be difficult to locate it using Time Machine. You can use Spotlight searching from within Time Machine to try to locate the file (using Quick Look to help this procedure); however, there are times when documents have been placed in the trash before Time Machine can back them up elsewhere.

Generally because of odd issues like this, storing items in the Trash is not recommended, but luckily as long as Time Machine has backed up the system while the items were in the trash then a backup does exist for it that can be recovered. Unfortunately doing so is not straightforward, because if you open the Trash and invoke Time Machine then the folder will revert to your Home directory as Time Machine activates, leaving you with no view of the trash folder backups to browse.

Hidden Trash folders
The trash folders in OS X are hidden. In this example the .Trashes folder is at the root of a USB flash drive, and within it the folder '502' is the user ID number for the current account (click for larger view).

The Trash folder system in OS X is a hidden set of directories that includes the home directory trash folder and the user's trash folders that are stored at the root of each locally mounted filesystem. When you open the Trash and view its contents, OS X shows you the contents of all of these trash folders, and emptying the trash subsequently empties these trash folders. These hidden folders are stored in the following locations (hidden because they have a period in front of their names):

/Macintosh HD/Users/USERNAME/.Trash
/HARDDRIVE/.Trashes/UID

To recover a file from within these directories, you must first open the hidden trash folder directly within the Finder, instead of opening the Trash in the Dock, and you must know which trash folder to open. For the most part, trashed items will be stored in the user's home directory, but if you quickly saved a file to an external or other secondary drive and then moved it to the trash, then the file will be in the ".Trashes" folder at the root of that drive.

Here is how to restore the file from these locations:

  1. Launch the Terminal application (located in the /Applications/Utilities/ folder).

  2. Enter the following command:

    open ~/.Trash

Terminal open command
Running the 'open' command on the hidden folders in the Terminal will open them in the Finder, where you can then use Time Machine to locate deleted files.

If the file was originally saved to an external hard drive, then ensure that drive is connected and do the following instead (takes a few more steps to target the desired hidden folder):

  1. Launch the Terminal application.

  2. Type "open" followed by a single space.

  3. Drag the mounted hard drive icon from the Finder to the Terminal window, and the full path to the hard drive should be entered.

  4. The full path will contain a trailing space, so press the delete key once to remove this space.

  5. With the space removed, copy and paste the following into the command (note the single quotes are in fact acute accent characters, if you want to type it in yourself):

    .Trashes/`id -u`

    When this is done the command should look something like the following:

    open /Volumes/DISKNAME/.Trashes/`id -u`

  6. Press Return to run the command, and the trash folder for your username will open (the name will be a number like 501 or 502).

Command Explanation:
The command used here will open the designated item with its default program. The default program is defined in the system's launch services, and for folders the default program is the Finder. In this situation we are using the command to point to the hidden .Trashes (or Trash in the home directory) folder. In the first option, the tilde key denotes the user's home directory, followed by the slash to reference items in the directory and then the ".Trash" filename. In the second command a similar thing is happening, except we use the in-line expression `id -u`. If you were to run "id -u" by itself on the command line it would give you the user's identification number as an output (such as 502). Since the .Trashes folder contains a subdirectory for each user that is named by that user's ID number, we can use the "id -u" command in this manner to dynamically insert the current user's ID number without knowing exactly what it is (it may be different for each system, and will be different for each user on a system). The use of the acute accents surrounding this command will execute it in-line, so the "open" command will interpret its output as the user's ID number.

When the folders are open in the Finder, go to the Time Machine menu and invoke Time Machine. The backups should now load while showing you the trash folder, and you can then navigate through Time Machine to find your lost file and restore it. The file will be restored to the Trash, from which you can move it to another location in the Finder and open it.



Questions? Comments? Have a fix? Post them below or e-mail us!
Be sure to check us out on Twitter and the CNET Mac forums.