A bit of Linux – An alternative to rm command

I remember once during an assignment submission, a friend of mine issued one of the most sorry commands on the linux shell where he had placed his programs, the command was rm *.:P. Well, understandably he was trying to issue a command like rm *.txt but in a hurry he made this blunder mistake. He had to go through a lot of trouble after that. When I came to know about it, I felt that it was really a blunder and could happen to anyone. With the load of programming assignments increasing such a stupid error could result in a lot of waste. So, I decided to address this problem and came up with a very short and sweet solution for it. I didn’t bother to look up over the internet for existing solutions because mine was fine for me :D. Today I just ran through that script and wished to share it here.

The first step is to create a Dustbin directory i.e. a place where you want your files, which are removed by rm command should be moved. Preferably create it in your home directory as a hidden directory eg. mkdir /home/avi/.dustbin .
Second, create a bash script and place the following code in it.

#!/bin/bash
for i in $*
do
        if [ ${i:0:1} != "-" ]; then # NOT an arguement to rm eg. -rf 
                `mv $i /home/avi/.dustbin`
        fi
done

Name this file as rm and place in any directory which you wouldn’t delete eg. ~/Documents and make this file an executable by issuing chmod +x rm

Next step is to edit the .bashrc file in your home directory. If there is a line which goes export PATH then before this line or else before the aliases write the following lines
PATH=/home/avi/Documents:$PATH
export $PATH

Do replace the avi with your home directory’s name 😛

Now your solution is ready. Whenever you’ll issue the command rm file.txt or rm * the files will be just moved to this Dustbin. It is like the recycle bin of windows but for the terminal. So, all the files/directories that you remove with the rm command will be moved here.

Now the hitch comes on how to delete these files permanently from .dustbin?? 😛 To do so you will have to issue command like /bin/rm -rf .dustbin. This will delete the entire dustbin. You can figure out now how you can customize it. I was looking for a way to provide a notification via a pop-up to the user when the dustbin grows very large. I found the zenity command for it and tried to place it in .bash_logout but things didn’t work out. I hope some of you people can help me find a good way to notify the user of cleaning up the dustbin.

Well, this might not be the best solution but being a newbie in linux, I enjoyed my little work. 🙂

Advertisements
%d bloggers like this: