Is there a Linux script / application which, instead of deleting files, moves them to a special “trash” location? I’d like this as a replacement for rm (maybe even aliasing the latter; there are pros and cons for that).

By “trash” I mean a special folder. A single mv $* ~/.trash is a first step, but ideally this should also handle trashing several files of the same name without overwriting older trashed files, and allow to restore files to their original location with a simple command (a kind of “undo”). Furthermore, it’d be nice if the trash was automatically emptied on reboot (or a similar mechanism to prevent endless growth).

Partial solutions for this exist, but the “restore” action in particular isn’t trivial. Are there any existing solutions for this which don’t rely on a trash system from a graphical shell?

(As an aside, there have been endless discussions whether this approach is justified, rather than using frequent backups and VCS. While those discussions have a point, I believe there’s still a niche for my request.)

@ephsmith Thanks, good link. The problem with those approaches though is that they are bound to specific desktop shell (what’s the correct term here?) implementations, something which I want to avoid.
–
Konrad RudolphJul 11 '12 at 9:32

Is moving files from any filesystem to your ~ intentional? Because some day you might be deleting a 4GB iso image residing on a dir mounted with sshfs from a really remote server.
–
Mischa ArefievJul 11 '12 at 11:40

@Mischa To be honest, I didn’t put that much thought into it. That said, it should work with the usual user’s rights, so the target needs to be a location that is writeable and shouldn’t require too much configuration.
–
Konrad RudolphJul 11 '12 at 11:43

Do whatever you want such as the solutions outlined in the answers below, but don't name it rm. As pointed out by others, renaming/repurposing standard commands leaves you vulnerable when you habitually try to use them on other systems, but it also will cause problems for anyone else (perhaps assisting you) using your system/account when unexpected results occur.
–
JoeJul 14 '12 at 20:43

There is also libtrash which moves all deleted files automatically to the trash via LD_PRELOAD (but it seems to have several bugs). Autotrash helps to clean the trash in an easy way.
–
jofelJul 11 '12 at 8:47

@jofel: libtrash has a really nice concept. A few layers deeper than the other approaches. It's a pity it is buggy (and does not seem very active).
–
zpeaJul 11 '12 at 10:29

2

@KonradRudolph: I meant that one gets used to the fact that rm (the replaced one) does not really delete anything, so that one is less careful, as a restore is always possible. Of course, using rm itself is not a bad thing, nor is getting used to it.
–
zpeaJul 11 '12 at 10:33

I’ve ended up using this solution, and disabling rm so I can’t use it accidentally (there’s still /bin/rm in case I really need it).
–
Konrad RudolphSep 5 '12 at 8:16

It doesn't seem to respond to params like -r or -f (it appears to essentially just be moving the file/directory to the ~/.Trash directory), but it won't override files with the same name (it appends "Copy" to like-named files/directories).

You can always call old rm by escaping it with a backslash, like this: \rm.

I don't know how to make the trash directory empty on reboot (depending on your system, you may have to look into the rc* scripts), but it could also be worthwhile to create a cron task that empties the directory periodically.

Unfortunately, that was the easy part … :/
–
Konrad RudolphJul 10 '12 at 22:11

This script could also create a text file in a hidden directory for each file which contains the directory it was in. A restore script could read the old location and move it back.
–
ephsmithJul 10 '12 at 22:20

This also has a hazard of multiple deleted files with the same name would collide in the trash directory, and only the last one "deleted" would survive to be able to be recovered.
–
killermistJul 10 '12 at 23:05

@killermist, yes. Of course one would need to do something additional with the move command. Name the "trashed" file whatever you want and keep the original path :| This all screams "why re-create the wheel". There are existing solutions to this problem.
–
ephsmithJul 11 '12 at 0:47

Also, use a different alias name. Work on another machine without your aliases, one call to rm and there go your files. del might be a better choice.
–
glenn jackmanJul 11 '12 at 0:51