LinuxQuestions.org
Support LQ: Use code LQ3 and save $3 on Domain Registration
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices



Reply
 
Search this Thread
Old 06-22-2010, 02:46 AM   #1
ssvirdi
Member
 
Registered: Apr 2009
Location: Ludhiana, Punjab, India.
Distribution: Ubuntu
Posts: 45

Rep: Reputation: 15
Smile 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
 
Old 06-22-2010, 03:14 AM   #2
bathory
Guru
 
Registered: Jun 2004
Location: Piraeus
Distribution: Slackware
Posts: 10,973

Rep: Reputation: 1343Reputation: 1343Reputation: 1343Reputation: 1343Reputation: 1343Reputation: 1343Reputation: 1343Reputation: 1343Reputation: 1343Reputation: 1343
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
 
Old 06-22-2010, 04:17 AM   #3
colucix
Moderator
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957
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.
 
Old 06-22-2010, 04:23 AM   #4
konsolebox
Senior Member
 
Registered: Oct 2005
Distribution: Gentoo, Slackware, LFS
Posts: 2,245
Blog Entries: 16

Rep: Reputation: 233Reputation: 233Reputation: 233
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.
 
Old 06-22-2010, 04:41 AM   #5
unSpawn
Moderator
 
Registered: May 2001
Posts: 27,693
Blog Entries: 54

Rep: Reputation: 2961Reputation: 2961Reputation: 2961Reputation: 2961Reputation: 2961Reputation: 2961Reputation: 2961Reputation: 2961Reputation: 2961Reputation: 2961Reputation: 2961
Quote:
Originally Posted by ssvirdi View Post
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.
 
Old 06-22-2010, 04:56 AM   #6
XavierP
Moderator
 
Registered: Nov 2002
Location: Kent, England
Distribution: Lubuntu
Posts: 19,176
Blog Entries: 4

Rep: Reputation: 430Reputation: 430Reputation: 430Reputation: 430Reputation: 430
Moved: This thread is more suitable in Linux-Newbie and has been moved accordingly to help your thread/question get the exposure it deserves.
 
Old 06-22-2010, 05:38 AM   #7
cantab
Member
 
Registered: Oct 2009
Location: England
Distribution: *buntu, Vector
Posts: 499

Rep: Reputation: 102Reputation: 102
Quote:
Originally Posted by konsolebox View Post
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.
 
Old 06-22-2010, 07:31 AM   #8
Agrouf
Senior Member
 
Registered: Sep 2005
Location: France
Distribution: LFS
Posts: 1,591

Rep: Reputation: 79
Quote:
Originally Posted by konsolebox View Post
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.
 
Old 06-22-2010, 09:11 AM   #9
ssvirdi
Member
 
Registered: Apr 2009
Location: Ludhiana, Punjab, India.
Distribution: Ubuntu
Posts: 45

Original Poster
Rep: Reputation: 15
thanks for your questions, but I am quite confuse.
Please provide a viable solution to do this.
 
Old 06-22-2010, 09:20 AM   #10
ssvirdi
Member
 
Registered: Apr 2009
Location: Ludhiana, Punjab, India.
Distribution: Ubuntu
Posts: 45

Original Poster
Rep: Reputation: 15
sorry for spelling mistake

thanks for your answers, but I am quite confuse.
Please provide a viable solution to do this.
 
Old 06-22-2010, 01:23 PM   #11
Agrouf
Senior Member
 
Registered: Sep 2005
Location: France
Distribution: LFS
Posts: 1,591

Rep: Reputation: 79
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

Last edited by Agrouf; 06-22-2010 at 01:24 PM.
 
Old 06-23-2010, 12:32 AM   #12
konsolebox
Senior Member
 
Registered: Oct 2005
Distribution: Gentoo, Slackware, LFS
Posts: 2,245
Blog Entries: 16

Rep: Reputation: 233Reputation: 233Reputation: 233
Quote:
Originally Posted by Agrouf View Post
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.

Last edited by konsolebox; 06-23-2010 at 12:35 AM.
 
Old 06-23-2010, 01:25 AM   #13
Agrouf
Senior Member
 
Registered: Sep 2005
Location: France
Distribution: LFS
Posts: 1,591

Rep: Reputation: 79
Quote:
Originally Posted by konsolebox View Post
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.
 
Old 06-23-2010, 01:47 AM   #14
konsolebox
Senior Member
 
Registered: Oct 2005
Distribution: Gentoo, Slackware, LFS
Posts: 2,245
Blog Entries: 16

Rep: Reputation: 233Reputation: 233Reputation: 233
Quote:
Originally Posted by Agrouf View Post
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.

Last edited by konsolebox; 06-23-2010 at 01:53 AM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Using sed to modify command output brianmcgee Programming 7 06-14-2010 08:49 AM
how to modify the output of df command anurupr Linux - Newbie 14 03-21-2010 02:21 PM
Command syntax to modify multiple files sg_sonic Linux - Newbie 5 09-27-2009 03:28 PM
how to ; modify system uid max range in command in aix / avklinux AIX 2 03-01-2009 01:03 PM
vi help, how to modify previous command ufmale Linux - Newbie 1 09-05-2008 01:20 PM


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

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration