I was cleaning up some deprecated and antiquated script files after refactoring them into a new one. Then I did something dumb. rm objectname* which included my new script I had worked on for about 4 hours. There is no undo for rm in the command line. This is not the first time this has happened to me. The lesson here, according to many linux msg boreds, is to learn not to do that or quit. I figured there has to be a way to backup deleted files.

If you have root access I suggest you install trash-cl, but if you are on a shared hosting account like I am this might be of help to you.

I decided to create a command called tr which is short for trash. I created a directory for the trash mkdir ~/.trash and in .bash_profile I added this code –
move_to_trash() {
CWD="`pwd`"
CWD="${CWD##/*/}"
if [ "$CWD" == ".trash" ]
then
rm -fr ~/.trash/*
else
mv -i $1 ~/.trash/
fi
}
alias tr=move_to_trash

After saving your update you can source ~/.bash_profile and see if it’s working without reconnecting with the server. You can tr files just like you would rm them, except they are actually moved to the .trash directory. If you want to empty the .trash then cd ~/.trash and run tr. All gone! Maybe in a future version I’ll add recursive rm behaviours.

They’ll tell you this is a bad idea; that it creates a bad habit; that you should respect the power of rm and pay better attention to what you’re doing. That’s recockulous. Accidents happen. Especially with a fist full similarly named files.

Now here is a related video! :D

Filed under — bash tricks
Tags — , , , , ,

Leave a Reply