Geek tip of the week! Alias rm in Linux

There have been too many non-geek posts this week. There’s even sports and stuff… amazing… for 6 biochemists…SO! To get back the honour and pride! :D

When using Linux and more specifically the command line, you often use rm. rm means remove. And it does remove. There’s no recycle bin nor Trash folder to move things to. But we can make it happen. I found this script in the Ubuntu forums:

#!/bin/sh
# trashit

# original script
#    http://www.macosxhints.com/article.php?story=20030217172653485
#    author: Shane Celis <shane (at) gnufoo (dot) org>
#
# Sun, 20-May-2007; 06:47:22
#    minor changes...

if [ $# -eq 0 ]; then
 echo "usage: trashit <files...>" >&2
 exit 2;
fi

for file in "$@"; do
 # get just file name
 destfile="`basename \"$file\"`"
 suffix='';
 i=0;

 # If that file already exists, change the name
 while [ -e "$HOME/.Trash/${destfile}${suffix}" ]; do
 suffix=" - copy $i";
 i=`expr $i + 1`
 done

 mv -vi "$file" "$HOME/.Trash/${destfile}${suffix}"
done

You basically save this file to your home folder under the name trashit, then edit your .bashrc file and add a line to the end of it saying:

alias rm='~./trashit'

From now on, every file you delete with rm will be instead moved to a directory called .Trash in your home directory. Don’t forget to create it first! But since this will pretty much be full if you remove things often, you should have an auto cleaning method.
Edit your crontab and add the following line:

* * * * 1 /bin/rm ~/.Trash/*

This makes your “Trash” directory to be cleaned every first day of the week. And that’s it :) Remember to set the ‘trashit’ file executable with chmod a+x trashit otherwise it won’t work!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s