What is the trash restore command?

The trash restore command is used to restore files that have been deleted and moved to the trash or recycle bin on Mac and Linux operating systems. When a file is deleted on a Mac or Linux system, it is not immediately removed from the hard drive. Instead, it is moved to a special hidden folder called the Trash or Recycle Bin. This allows users to easily restore accidentally deleted files. The trash restore command provides a way to restore these deleted files from the command line.

How does the trash work on Mac and Linux?

On Mac OS X and Linux distributions like Ubuntu, when a user deletes a file, the file is moved to a hidden .Trash folder located at the root of each mounted volume. So if a file is deleted from the user’s home directory, it would be moved to /home//.Trash. If deleted from an external drive, it would go to /Volumes//.Trash.

The .Trash folder is structured like a normal home directory, with subdirectories for each user. So a file deleted by a user would go to /home//.Trash/. There is also a files folder that contains the deleted files and an info folder containing a metadata file for each deleted file with details like the original path and deletion date.

The trash provides a safety net for accidentally deleted files. Files in the trash can be restored up until the trash is emptied. Emptying the trash deletes the files permanently by removing the .Trash folder contents. However, with the trash restore command, files can be recovered even after emptying the trash.

What is the trash restore command?

The trash restore command allows restoring deleted files from the trash via the command line. It works by accessing the metadata files still kept in the .Trash info directory even after emptying the trash. Here are the main trash restore commands:

Restore a File from Trash

To restore a specific file from the trash, use:

restore-trash /path/to/file

For example:

restore-trash /home/user/.Trash/info/important-file.txt.trashinfo

This finds the .trashinfo metadata file for important-file.txt and restores the deleted file specified within it.

Restore All Files for a User

To restore all deleted files for a user, omit the file path:

restore-trash

This recursively restores all files and folders within the user’s .Trash info directory.

Restore from a Specific Drive Trash

To restore from an external drive’s trash, specify the mount point path:

restore-trash /Volumes/External/name/.Trash

This will restore all deleted files for all users from the external drive’s .Trash folder.

How does the trash restore command work?

The trash restore command works by utilizing the .trashinfo metadata files kept in the hidden .Trash/info directory. Even after emptying the trash, these metadata files remain on the disk.

Each .trashinfo file contains the original path of the deleted file and directory. The restore command reads this original path and copies the file back to its original location. Any intermediate directories are also restored as needed.

In this way, the trash restore command can resurrect files even after the trash has been emptied. As long as the .trashinfo files remain, the data can be recovered.

When would you use the trash restore command?

Here are some common situations when the trash restore command would be useful:

  • Recovering accidentally deleted files after emptying the trash
  • Recovering files deleted by another user that you need to restore
  • Recovering important files and directories deleted a while ago
  • Restoring files from an external drive or USB stick’s trash
  • Automating bulk restoration of files instead of clicking through a graphical restore interface

The trash restore command gives you access to deleted files even after emptying the trash. It’s a useful data recovery tool for the command line.

Requirements for using the trash restore command

Here are some key requirements to use the trash restore command effectively:

  • The trash restore utility must be installed – this is included by default on most Linux distributions
  • You must have permissions to access the .Trash folder for the user whose deleted files you are restoring
  • There must be sufficient free disk space to restore the files to their original locations
  • The .trashinfo metadata files must still be present in the .Trash/info folder
  • The original mount points for external drives must still be present

As long as these requirements are met, the trash restore command can reliably recover deleted files.

Trash Restore Command Examples

Here are some examples of using the trash restore command in different scenarios:

Restore a Single File

restore-trash /home/user/.Trash/files/notes.txt.trashinfo

Restores the deleted notes.txt file for the user.

Restore Files for Another User

sudo restore-trash /home/otheruser/.Trash/info

Run as root or sudo to restore all of another user’s deleted files.

Restore from External Drive Trash

restore-trash /Volumes/External/name/.Trash

Restores deleted files from an external drive mounted at /Volumes/External/name.

Restore All Users’ Trash

sudo restore-trash /.Trash 

Globally restores deleted files for all users on the system when run as root.

Alternative Ways to Restore Deleted Files

In addition to the trash restore command, here are some other ways to recover deleted files on Linux and Mac OS X:

  • Use data recovery software that can scan disk partitions and resurrect deleted files.
  • Find and copy files from visible Time Machine or snapshot backups.
  • Recover files from any available full disk image backups.
  • Use debugfs or extundelete utilities on ext file systems to recover deleted file inodes.
  • Recover deleted files still open by a process using lsof +L1.

However, the trash restore command remains one of the simplest and most convenient ways to recover deleted files on Linux and Mac systems.

Trash Restore Command Best Practices

Here are some best practices when using the trash restore command:

  • Act quickly – Restore files as soon as possible before .trashinfo metadata files get purged from the system.
  • Verify disk space – Check that you have adequate disk space before restoring files.
  • Test on copies first – Practice running the command on copies of .trashinfo files rather than actual ones.
  • Use absolute paths – Use full absolute paths to trashinfo files rather than relative paths.
  • Restore to separate locations – Restore to a different location rather than original paths when possible.

Following these best practices will give you the best chance of successfully restoring deleted files with the trash restore command.

Trash Restore Command on Mac vs Linux

The trash restore command works similarly on Mac OS X and Linux distributions like Ubuntu. However, here are some differences:

Feature Mac Trash Linux Trash
Location .Trash folder in each volume .Trash folder in home and mount points
Utility name trash restore-trash
Security Per-user .Trash folders Global readable .Trash info
Restore command trash -–restore restore-trash

So the trash restore process is very similar, with the main difference being the slightly different command names and utilities used on each system.

Conclusion

The trash restore command provides a handy way to recover deleted files on Linux and Mac systems. By utilizing the .trashinfo metadata files, it can resurrect data even after emptying the trash. Some key points to remember include:

  • The restore-trash command restores from the hidden .Trash folder
  • It can restore individual files or entire directories
  • You need access to the .Trash folder and original mount points
  • It works even after emptying the trash as long as .trashinfo files remain
  • Use absolute paths and follow best practices for best results

So in summary, the trash restore command is an invaluable data recovery tool on Linux and Mac. Learn to use it effectively, and you can save yourself from catastrophe when important files get deleted accidentally.