LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
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, 01:46 AM   #1
ssvirdi
Member
 
Registered: Apr 2009
Location: Ludhiana, Punjab, India.
Distribution: Ubuntu
Posts: 47

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, 02:14 AM   #2
bathory
LQ Guru
 
Registered: Jun 2004
Location: Piraeus
Distribution: Slackware
Posts: 13,163
Blog Entries: 1

Rep: Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032
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, 03:17 AM   #3
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
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, 03:23 AM   #4
konsolebox
Senior Member
 
Registered: Oct 2005
Distribution: Gentoo, Slackware, LFS
Posts: 2,248
Blog Entries: 8

Rep: Reputation: 235Reputation: 235Reputation: 235
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, 03:41 AM   #5
unSpawn
Moderator
 
Registered: May 2001
Posts: 29,415
Blog Entries: 55

Rep: Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600
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, 03:56 AM   #6
XavierP
Moderator
 
Registered: Nov 2002
Location: Kent, England
Distribution: Debian Testing
Posts: 19,192
Blog Entries: 4

Rep: Reputation: 475Reputation: 475Reputation: 475Reputation: 475Reputation: 475
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, 04:38 AM   #7
cantab
Member
 
Registered: Oct 2009
Location: England
Distribution: Kubuntu, Ubuntu, Debian, Proxmox.
Posts: 553

Rep: Reputation: 115Reputation: 115
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, 06:31 AM   #8
Agrouf
Senior Member
 
Registered: Sep 2005
Location: France
Distribution: LFS
Posts: 1,596

Rep: Reputation: 80
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, 08:11 AM   #9
ssvirdi
Member
 
Registered: Apr 2009
Location: Ludhiana, Punjab, India.
Distribution: Ubuntu
Posts: 47

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, 08:20 AM   #10
ssvirdi
Member
 
Registered: Apr 2009
Location: Ludhiana, Punjab, India.
Distribution: Ubuntu
Posts: 47

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, 12:23 PM   #11
Agrouf
Senior Member
 
Registered: Sep 2005
Location: France
Distribution: LFS
Posts: 1,596

Rep: Reputation: 80
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 12:24 PM.
 
Old 06-22-2010, 11:32 PM   #12
konsolebox
Senior Member
 
Registered: Oct 2005
Distribution: Gentoo, Slackware, LFS
Posts: 2,248
Blog Entries: 8

Rep: Reputation: 235Reputation: 235Reputation: 235
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-22-2010 at 11:35 PM.
 
Old 06-23-2010, 12:25 AM   #13
Agrouf
Senior Member
 
Registered: Sep 2005
Location: France
Distribution: LFS
Posts: 1,596

Rep: Reputation: 80
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, 12:47 AM   #14
konsolebox
Senior Member
 
Registered: Oct 2005
Distribution: Gentoo, Slackware, LFS
Posts: 2,248
Blog Entries: 8

Rep: Reputation: 235Reputation: 235Reputation: 235
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 12:53 AM.
 
  


Reply



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 07:49 AM
how to modify the output of df command anurupr Linux - Newbie 14 03-21-2010 01:21 PM
Command syntax to modify multiple files sg_sonic Linux - Newbie 5 09-27-2009 02:28 PM
how to ; modify system uid max range in command in aix / avklinux AIX 2 03-01-2009 12:03 PM
vi help, how to modify previous command ufmale Linux - Newbie 1 09-05-2008 12:20 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 05:57 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
Open Source Consulting | Domain Registration