# Undo the Linux trash command

Can we undo operations done in a terminal, for example, file deletion through rm?

Solutions obtained:

1. Aliasing
2. Undelete utilities
3. Backup utilities
4. LibTrash
5. Versioning (FUSE)
-
On rm: It unlinks a file from it's inode. The question: "Where do files go when the rmcommand is issued" -> unix.stackexchange.com/questions/10883/… might also be a good read for you. – erch Dec 8 '13 at 15:52

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. - +1 for a much better answer than mine =) – The How-To Geek Aug 28 '09 at 6:27 the second alias is very, very smart. +1. – LiraNuna Aug 28 '09 at 7:58 trash() { mv$@ ~/.Trash; } # bash function, not an alias. Changing the expected behavior of rm is a bad idea, IMHO. – Richard Hoskins Aug 28 '09 at 12:43
The risk with aliasing rm to rm -i is that you get used to the safety net that it gives you. Then you go onto another machine, which doesn't have that alias... – Dentrasi Aug 29 '09 at 9:35
For the reasons that Richard and Dentrasi mention, I prefer to create a custom function or to alias rmi -> rm -i. It's really a mistake to simply get in the way of an existing program by aliasing over it. – Telemachus Aug 31 '09 at 2:15

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.

-
Yes, there is a trash for the command line. See trash-cli in another answer. – Sparhawk Feb 28 '14 at 4:19
Trash-cli is so good :) and uses Trashlib, better than simply moving files. – erm3nda Jan 11 at 22:15

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.

-
trash-cli provides also works with GNOME trash and it's designed to provide rm options compatibility (for the aliasing). – Andrea Francia Dec 2 '09 at 8:04
Don't forget to alias rm=trash so that your typical command-line screw-ups come with undo buttons. – Ryan Thompson Feb 19 '10 at 23:25
I think alias rm=trash is potentially dangerous, if there is any chance you will use someone else's system one day and forget (or ssh). Much safer to just get used to writing trash instead of rm. – Sparhawk Feb 28 '14 at 4:20

You could make rm an alias for the trash command (You will need to install trash first.) Add this to your ~/.bashrc :

alias rm='trash'


This is preferable to alias rm='mv --target-directory=$HOME/.Trash' since ~/.Trash is NOT the trash folder for gnome. It is better IMHO to let trash figure out where the actual trash folder is. btw I would have posted this in a comment but I don't have enough rep. - +1 for "since ~/.Trash is NOT the trash folder for gnome." However, rm shouldn't be aliased to trash either. You should just use trash instead of rm if that's what you want. – nagul Aug 31 '09 at 9:35 True, but for some people old habits die hard. – Alvin Row Aug 31 '09 at 15:50 'rmtrash' is another option. – Itachi Jun 12 '15 at 6:10 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. - There's a larger question here that's worth addressing. Shell commands are not chatty (they don't double check what you want), and they expect you to know what you're doing. This is fundamental to how they are designed. It's a feature, not a bug. Some people feel macho when they use such commands, which I think is pretty silly, but it is important to understand the dangers. You can do a great deal of damage in a terminal, even if you're not root. I think you probably really just cared about rm, but since you said "Can we undo the the operations done in terminal", I thought this was worth saying. The general answer is no, you can't. - Option 1: See Undelete Linux Files from an ext2 File System. This page points to a program, written by Sebastian Hetze of the LunetIX company, that (as the title suggests) undeletes recently deleted files from an ext2 filesystem. Example usage: # undelete -d /dev/hdc3 -a 10  Warnings: • The original web site is gone. The link, above, is to the Internet Archive. • The site(s) are in a mixture of English and German. • As stated above, the tool is designed specifically for the ext2 filesystem. It is unlikely to work on any other filesystem type; especially not ones other than extN. Option 2: I have rsnapshot (rsync) running on my machine which makes snapshots hourly of my selected folders. It incrementally does this every hour, 2 hours or whatever you tell CRON to do. After a full day it recycles these snapshots into one daily snapshot and after 7 days in a weekly so on and so on. This makes me able to go back in time for about a month or so for every hour! It is pretty good with disk space as it creates symbolic links to files which never changed... - Just to clarify, #1 is specific to ext2 filesystems. It wouldn't work on an ext3 filesystem. – nagul Aug 31 '09 at 9:35 Recover using grep on /dev/partition (Linux or Unix Recover deleted files – undelete files), grep -b 'search-text' /dev/partition > file.txt  Just a try. - There exist undelete utilities for ext2, but most other Linux filesystems are stuck in the Stone Age and don't have any advanced usability features. Sad state of affairs considering gigantic drives with enough space to never delete a file again are commonplace. So you are stuck with three options: 1. Do backup regularly, for example with a command like: rsync -axvRP --backup --backupdir=/backup/incr/$(date -I) /home/ /backup/root/

2. Use a version control tool such as git for all your work. While this will not protect against against a crazy rm -r that kills the repository, it will protect against regular troubles as you will be using git rm not raw rm.

3. Be extra careful and don't trust too much in rm -i, trash-cli and friends, as most data you will lose on the shell you will not lose by accidental rm, but by misdirected pipes, mistyped output files, misdirected mv and stuff, i.e., things that will overwrite your data, not just delete it.

Do all three for maximum amount of safety.

-
It's pretty hilarious to call ext2 modern. The newer filesystems use more complicated on-disk formats than ext2/3, and had to give up easy-to-find locations where you might find files to un-delete. They're designed for people who make backups of things they care about. BTW, I always use mv and cp -i, since I don't normally want to clobber anything. I normally type \rm, because I have rm aliased to rm -i, too, but I don't want to answer its question. – Peter Cordes Dec 12 '09 at 4:19

check this ... migh be helpful http://artmees.github.io/rm/

suppose you did

rm very_important_file


from the terminal. recovering this file is a tedious and not always successful process

rm very_important_file
mv very_important_file ~/.Trash/


are equivalent. the script handles more cases and doesn't alter your system rm at all and that is because it's put into the user local bin folder so it shadows the system rm and yet doesn't affect it or disable using it

this is a refined aliasing approach but without losing any feature

-
It'd be good if you could add some explanation as to what this does and how it applies to the question, maybe add an example, etc. – slhck Dec 7 '13 at 12:24

TestDisk can undelete files from FAT, exFAT, NTFS and ext2 filesystems.

-