Undo the Linux trash command

Solution 1:

There is no general "undo" for every operation in terminal.  If you wish to recover a file after using rm you will need to look into recovery software.

An option to prevent you from future mistakes is to make aliases for alternative commands to remove files.  Add them to your ~/.bashrc and get into the habit of using them instead of rm.

  1. alias rmi='rm -i'

    If you use rmi, you will be prompted for confirmation of future deletes.  Try to avoid developing the habit of pressing y immediately after you issue an rmi command, as that will defeat the purpose.

  2. You could also move files deleted by the trsh command in terminal to your recycle bin (on KDE and Gnome):

    alias trsh='mv --target-directory="$HOME/.Trash"'
    

    If you use trsh, you will have a limited "undelete" capability.  Beware that

    trsh dir1/file1 dir2/file1
    

    may still cause unrecoverable data loss.

Solution 2:

There is no recycle bin for the command line.

You could try some of the various undelete utilities, but there's no guarantee that they would work.

Solution 3:

You could use trash-cli if you use KDE when you run a gui. This is command line utility to delete/restore using the KDE trash facilities.

Solution 4:

Two more technical solutions have not be named yet:

  1. libtrash: A dynamic library which can be preloaded in your shell which intercepts the deleting/removing syscalls and moves the files to a trash folder instead (much like an alias but works for any application when preloaded).
  2. A versioning file system. If you delete (or edit, or copy, or ...) a file, you can just revert to an old state. This could be done with a FUSE filesystem and one of its versioning filesystems.