LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   How to modify rm command ? (https://www.linuxquestions.org/questions/linux-newbie-8/how-to-modify-rm-command-815581/)

ssvirdi 06-22-2010 01:46 AM

How to modify rm command ?
 
Dear Friends,

When we execute rm command then the target files or directories are permanently removed from our computer.

Is there any way my which when we execute rm command then the files or directories moved to Trash.

please help me, its very important for me.

Thanks in advance

bathory 06-22-2010 02:14 AM

Hi,

You can create a script rm.sh (and put it for example in /usr/bin):
Code:

#!/bin/sh
mv $1 ~/Trash/$1

and use alias to alias rm to the script:
Code:

alias rm='/usr/bin/rm.sh'
Regards

colucix 06-22-2010 03:17 AM

Following the suggestion by bathory, you may want to be able to move more files at once (for example when using wildcards). In this case better to use $@ to catch all the command line arguments:
Code:

mv "$@" ~/.Trash
Moreover, since emptying trash is something that many users forget about, you may want to add a check on the Trash size and/or on the filesystem percent usage. Eventually the user may be warned and receive advice to empty the trash bin. Finally, take in mind that files with the same name will be overwritten, so that older versions will be definitively lost.

konsolebox 06-22-2010 03:23 AM

Sometimes that's not a good idea though since there are some system scripts like boot scripts that may also use the rm command to permanently delete files like temporary files. It's probably better to create another similar script utility with different filename like /usr/bin/trash.

unSpawn 06-22-2010 03:41 AM

Quote:

Originally Posted by ssvirdi (Post 4011024)
Is there any way my which when we execute rm command then the files or directories moved to Trash.

This question turns up once in a while, was talked about recently, so searching LQ will yield results. To keep you from reinventing the wheel see http://www.linuxquestions.org/questi...9/#post3987860.

XavierP 06-22-2010 03:56 AM

Moved: This thread is more suitable in Linux-Newbie and has been moved accordingly to help your thread/question get the exposure it deserves.

cantab 06-22-2010 04:38 AM

Quote:

Originally Posted by konsolebox (Post 4011092)
Sometimes that's not a good idea though since there are some system scripts like boot scripts that may also use the rm command to permanently delete files like temporary files. It's probably better to create another similar script utility with different filename like /usr/bin/trash.

Agreed, I would not change the behaviour of a fundamental command like rm in such a way. Creating a new 'trash' command would be better.

Agrouf 06-22-2010 06:31 AM

Quote:

Originally Posted by konsolebox (Post 4011092)
Sometimes that's not a good idea though since there are some system scripts like boot scripts that may also use the rm command to permanently delete files like temporary files. It's probably better to create another similar script utility with different filename like /usr/bin/trash.

Sure, but putting an alias in his .bash_profile does not affect how rm works in boot scripts and system scripts in general.

ssvirdi 06-22-2010 08:11 AM

thanks for your questions, but I am quite confuse.
Please provide a viable solution to do this.

ssvirdi 06-22-2010 08:20 AM

sorry for spelling mistake

thanks for your answers, but I am quite confuse.
Please provide a viable solution to do this.

Agrouf 06-22-2010 12:23 PM

Solution is in posts #2 and #3. Put the alias in your .bash_profile

1. Create the command /usr/bin/rm.sh as follow (as root):
Code:

cat >/usr/bin/rm.sh <<EOF
#!/bin/sh
[ ! -d ~/.trash ] && mkdir ~/.trash
mv $* ~/.trash
EOF
chmod a+x /usr/bin/rm.sh

2. Append that command to your .bash_profile (as normal user)
Code:

echo "alias rm='/usr/bin/rm.sh'" >> ~/.bash_profile

konsolebox 06-22-2010 11:32 PM

Quote:

Originally Posted by Agrouf (Post 4011263)
Sure, but putting an alias in his .bash_profile does not affect how rm works in boot scripts and system scripts in general.

Well how bout the other scripts that will run after .bash_profile is loaded? e.g. co-shellscripts run by user apps?

Also I suggest that it's better to have functions instead of aliases since sometimes aliases do not work the way they should be. Perhaps at least on the earlier versions of bash?

Edit: Oh I forgot. Yeah right. Functions and aliases are not exported so this can be safely done.

Agrouf 06-23-2010 12:25 AM

Quote:

Originally Posted by konsolebox (Post 4012177)
Well how bout the other scripts that will run after .bash_profile is loaded? e.g. co-shellscripts run by user apps?

Also I suggest that it's better to have functions instead of aliases since sometimes aliases do not work the way they should be. Perhaps at least on the earlier versions of bash?

Edit: Oh I forgot. Yeah right. Functions and aliases are not exported so this can be safely done.

Not only that, but .bash_profile is not executed by scripts run by user apps. This is the difference between .bash_profile and .barhrc. .bash_profile is only executed in interactive mode.

konsolebox 06-23-2010 12:47 AM

Quote:

Originally Posted by Agrouf (Post 4012214)
Not only that, but .bash_profile is not executed by scripts run by user apps. This is the difference between .bash_profile and .barhrc. .bash_profile is only executed in interactive mode.

No I did not meant that scripts will call .bash_profile, what I meant is what will be in the shell environment and shared to subscripts after it is called;.. but it's ok since it won't be exported or shared anyway.

Edit: Or maybe you meant a different angle.. well ok.

Actually we don't even have to think about it since sometimes .bashrc is not also called. What really mattered was the effect after a new virtual version of rm is created.


All times are GMT -5. The time now is 11:34 PM.