Command line trash/recycle script

:: linux, tricks

A while back, I got really sick of sometimes accidentally rm-ing a file. I thought “Woe is me, if only I had a command that, instead, hid the file away from me, in a place I knew of but didn’t really thing about, so I could recover it if I wanted it.”.

I googled around for a bit and the best I found was a terrible approximation at a script to move files into a Trash folder. It didn’t work when there were spaces or funny symbols, it didn’t have exactly the same interface as rm, making it hard to just alias to rm, and it didn’t handle error very well.

And honestly, I’ve come to think I don’t even want that. What I want is a script that just renames the file, to something like .filename.timestamp.trash. Now it’s hidden, and in the same convenient place if I need to recover it. I could even have multiple versions in the same place! Sure, maybe things get cluttered, but a single

1
find ~/ iname="*.trash" -exec /bin/rm -f {} \; 

cleared them all out. Alias that to ‘empty’ and I’d be set!

But woe is me, no such script exists… oh, until about 10 minutes ago. I finally got around to writing it. 400 lines of beautiful bash! The same interface as rm, even with the ability to move and symlink files to a .trash folder, in case you don’t agree with me that they should just stay in the same damn folder. Handle filenames with spaces and funny characters, handles globs, did I mention same interface as rm?

I may get around to writing this in a better language, and maybe adding some things like a recover command that will display all the versions of the file. Maybe I’ll even add more documentation (I mean, it’s got a pretty thorough —help but…).

For now, here’s a github: https://github.com/bluephoenix47/trash.sh