LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices

Reply
 
LinkBack Search this Thread
Old 02-18-2010, 02:07 PM   #1
jaikris
Member
 
Registered: Sep 2009
Posts: 31

Rep: Reputation: 15
Question emulating "rm " command


I just need to edit the "rm " command such that it should move all the files / dir to a particular folder instead of deleting.

Can anyone guide me to edit the source code for rm , such that it should preserve all the deleted files / dir.
 
Old 02-18-2010, 02:16 PM   #2
dadrunamok
Member
 
Registered: Dec 2009
Location: Maryland, USA
Distribution: Ubuntu 11.04, that alternative OS from Washington State
Posts: 31

Rep: Reputation: 16
Why not use cp or mv? They already do that job (cp copies files and mv moves them).
 
Old 02-18-2010, 02:25 PM   #3
tlgrok
LQ Newbie
 
Registered: Feb 2010
Posts: 2

Rep: Reputation: 0
Here is an emulated recyle bin with a few issues, which you could use as a start:

http://www.linuxquestions.org/questi...4/#post1205474
 
Old 02-19-2010, 10:00 PM   #4
jaikris
Member
 
Registered: Sep 2009
Posts: 31

Original Poster
Rep: Reputation: 15
Hi everyone!

I am trying to write a bash script to emulate the rm command, I want to save all files I delete, a bit like the recycle bin in a windows OS.

The script that i got is not working properly , can any one pls guide me on this..

#!/bin/bash
# program to emulate the "rm" command in UNIX.
# less the endless sp

# CREATE TRASH FOLDER

if ! [ -d "$HOME/deleted" ] ; then
mkdir $HOME/deleted
fi

# INITIALIZE VARIABLES
OPT=-
NO_ARGS=0
FLAG_R=""
FLAG_F=""
FLAG_I=""
FLAG_V=""
TRASH=$HOME/deleted


# FUNCTIONS

function errorInvailidOpt() {

echo "rm: invalid option - $o"
echo "try \`rm -help\` for more information"
exit 0
}


function errorTooFew() {

echo "rm: too few arguments"
echo "try \`rm --help\` for more information"
}

function errorNoSuch() {

echo "rm: cannot remove $* : no such file or directory"
}


function writePro () {
echo -n "rm: remove write-protected file \`$*'?"
read ANSWER
if [ "$ANSWER" = "y" ] && [ "$FLAG_V" = "v" ] ; then
mv $OPTS $@ $TRASH 2>/dev/null
echo "removing \`$*'"
else
mv $OPTS $@ $TRASH 2>/dev/null

fi
}

function verbose () {
mv $@ $TRASH 2>/dev/null
echo "removing \`$*'"
}
function intVerbose () {
echo -n "rm: remove $* ?"
read ANSWER
if [ "$ANSWER" = "y" ] ; then
mv $@ $TRASH 2>/dev/null
echo "removing \`$*'"


fi
}

function int () {
echo -n "rm: remove $* ?"
read ANSWER
if [ "$ANSWER" = "y" ] ; then
mv $@ $TRASH 2>/dev/null
fi
}

function delete() {
while :
do case $OPTS in

v|ivf|vf|ifv|vif) verbose $@
break
;;
vfi|fvi|iv|vi|fiv) intVerbose $@
break
;;
f|fv|if) mv -f $@ $TRASH 2>/dev/null
break
;;
i) int $@
break
;;
r)mv $OPTS $@ $TRASH 2>/dev/null
break
;;
*)mv $@ $TRASH 2>/dev/null
break

esac
done

}


# GETOPTS

while getopts :rRfvi o
do case $o in
r|R)FLAG_R=""
;;
f) FLAG_F=f
;;
v) FLAG_V=v
;;
i) FLAG_I=i
;;
*) errorInvalidOpt

esac
done
shift `expr $OPTIND - 1`

# FLOW CONTROL

OPTS=$FLAG_R$FLAG_F$FLAG_I$FLAG_V

if [ "$#" -eq "$NO_ARGS" ] ; then
errorTooFew $@
elif ! [ -f "$1" ] && ! [ -d "$1" ]; then
errorNoSuch $@
elif ! [ -w "$1" ] ; then
writePro $@
else
delete $@
fi
 
Old 02-19-2010, 11:41 PM   #5
jaikris
Member
 
Registered: Sep 2009
Posts: 31

Original Poster
Rep: Reputation: 15
sorry for the inconvenience caused .. can any one guide me on this ...
 
Old 02-19-2010, 11:44 PM   #6
John VV
Guru
 
Registered: Aug 2005
Posts: 12,160

Rep: Reputation: 1598Reputation: 1598Reputation: 1598Reputation: 1598Reputation: 1598Reputation: 1598Reputation: 1598Reputation: 1598Reputation: 1598Reputation: 1598Reputation: 1598
have you looked through Gnome?
it auto puts stuff in the "trash"

hack that code , why reinvent the wheel
 
Old 02-20-2010, 01:49 AM   #7
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Servers: Debian Squeeze and Wheezy. Desktop: Slackware64 14.0. Netbook: Slackware 13.37
Posts: 8,523
Blog Entries: 27

Rep: Reputation: 1175Reputation: 1175Reputation: 1175Reputation: 1175Reputation: 1175Reputation: 1175Reputation: 1175Reputation: 1175Reputation: 1175
Quote:
Originally Posted by John VV View Post
have you looked through Gnome?
it auto puts stuff in the "trash"

hack that code , why reinvent the wheel
Hacking Gnome code? There's a job for life!
 
Old 02-21-2010, 10:01 PM   #8
jaikris
Member
 
Registered: Sep 2009
Posts: 31

Original Poster
Rep: Reputation: 15
i just customize the script as below , but still its not moving files to my destination folder /tmp/trash ,

can you pls review the below script and help me out ..

# INITIALIZE VARIABLES
OPT=-
NO_ARGS=0
FLAG_R=""
FLAG_F=""
FLAG_I=""
FLAG_V=""sh
TRASH=tmp/tr


# GETOPTS

while getopts :rRfvi jk
do case $jk in
r|R)FLAG_R=""
;;
f) FLAG_F=f
;;
v) FLAG_V=v
;;
i) FLAG_I=i
;;
*)
{

echo "rm: invalid option - $jk"
echo "try \`rm -help\` for more information"
exit 0
}

esac
done
shift `expr $OPTIND - 1`

# FLOW CONTROL

OPTS=$FLAG_R$FLAG_F$FLAG_I$FLAG_V


if [ "$#" -eq "$NO_ARGS" ] ; then

{
echo "rm: too few arguments"
echo "try \`rm --help\` for more information"
}

elif ! [ -f "$1" ] && ! [ -d "$1" ]; then

{
echo "rm: cannot remove $* : no such file or directory"
}

else
{
while :
do case $OPTS in

v|ivf|vf|ifv|vif)
{
mv $@ $TRASH 2>/dev/null
echo "removing \`$*'"
}

break;;
vfi|fvi|iv|vi|fiv) {
echo -n "rm: remove $* ?"
read ANSWER
if [ "$ANSWER" = "y" ] ; then
mv $@ $TRASH 2>/dev/null
echo "removing \`$*'"
fi
}
break;;
f|fv|if) mv -f $@ $TRASH 2>/dev/null
break;;

i)
{
echo -n "rm: remove $* ?"
read ANSWER
if [ "$ANSWER" = "y" ] ; then
mv $@ $TRASH 2>/dev/null
fi
}
break;;
r)mv $OPTS $@ $TRASH 2>/dev/null
break;;

*)mv $@ $TRASH 2>/dev/null
break

esac
done

}
fi
 
Old 02-22-2010, 01:05 AM   #9
chrism01
Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.5, Centos 5.10
Posts: 16,093

Rep: Reputation: 1995Reputation: 1995Reputation: 1995Reputation: 1995Reputation: 1995Reputation: 1995Reputation: 1995Reputation: 1995Reputation: 1995Reputation: 1995Reputation: 1995
1. post your code in code tags; its hard to read
2. use

set -xv

at the top to show you what's happening
3. cut the code to the bare minimum ie one option or even none to start with

4.
Quote:
FLAG_V=""sh
What is that supposed to be?
 
Old 02-22-2010, 01:17 AM   #10
micxz
Senior Member
 
Registered: Sep 2002
Location: CA
Distribution: openSuSE, Cent OS, Slackware
Posts: 1,131

Rep: Reputation: 75
Why not just make an alias:

alias rm='mv -t ~/Trash/'

add that to your .alias file or .bashrc done. if you use a alias file make sure in bashrc you have:

test -s ~/.alias && . ~/.alias || true
 
Old 02-22-2010, 03:50 AM   #11
i92guboj
Gentoo support team
 
Registered: May 2008
Location: Lucena, Córdoba (Spain)
Distribution: Gentoo
Posts: 4,029

Rep: Reputation: 368Reputation: 368Reputation: 368Reputation: 368
You should really concentrate on something simpler that you can understand. This script is obviously over the top for you, no pun intended, no one is born knowing everything, you need to learn one step at a time, just like we all did at one point. If you want to learn you should start with something simpler.

It's irrelevant if you implement this as an alias, a function or a separate script. The core of your script will be an "mv" command that will move the files to a given folder instead of rm'ing them. That's a single line, only one line of code. Find that, do it, and then you can start worrying about the rest. But building a skyscraper when you can't handle a single brick is quite ambitious.
 
Old 05-30-2010, 10:50 PM   #12
jaikris
Member
 
Registered: Sep 2009
Posts: 31

Original Poster
Rep: Reputation: 15
finally i could develop a script which meeting my expectation , while deleting instead of vanishing its keeping a zipped copy of files / dir to tone specified folder.

the problem is :
i have renamed the current /bin/rm to /bin/rm_old and kept my script as /bin/rm , while am using "rm" its executing my script and working fine . but while shutting down and booting up its creating so many problems , it seams so many dependencies with original rm.


how can i rename the original rm to some other name ( /bin/rm-old ) , if am doing this whether i should mention any where so that kernel can identify the original rm is in new name /bin/rm_old
 
Old 05-31-2010, 12:09 AM   #13
Nylex
LQ Addict
 
Registered: Jul 2003
Location: London, UK
Distribution: Slackware
Posts: 7,464

Rep: Reputation: Disabled
Quote:
Originally Posted by jaikris View Post
the problem is :
i have renamed the current /bin/rm to /bin/rm_old and kept my script as /bin/rm , while am using "rm" its executing my script and working fine . but while shutting down and booting up its creating so many problems , it seams so many dependencies with original rm.


how can i rename the original rm to some other name ( /bin/rm-old ) , if am doing this whether i should mention any where so that kernel can identify the original rm is in new name /bin/rm_old
You probably shouldn't do this, purely for the reason you've described - it causes problems. Why don't you do what i92guboj or micxz suggested above?
 
Old 05-31-2010, 12:50 AM   #14
jaikris
Member
 
Registered: Sep 2009
Posts: 31

Original Poster
Rep: Reputation: 15
Its regarding my MS projecct , can you pls suggest if we rename /bin/rm to /bin/rm_old what all we should do ...... also new script came to /bin/rm path also .... pls suggest ..
 
Old 05-31-2010, 01:41 AM   #15
micxz
Senior Member
 
Registered: Sep 2002
Location: CA
Distribution: openSuSE, Cent OS, Slackware
Posts: 1,131

Rep: Reputation: 75
What you should do is put rm back to where it was and just make a the rm in YOUR environment go to your script. e.g:
alias rm='/home/jaikris/bin/rm_newscript'
Does this make sense? Hope this helps'
 
  


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
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] When I type "sudo grub" it says "command not found" in Ubuntu 9.10 Live CD. msbstar Linux - Newbie 7 01-30-2010 11:05 AM
ns:"error when calling class OldSim"&tclsh:"invalid command+child process exits abn." shojaru Linux - Newbie 0 03-05-2009 04:23 AM
Command "mail" returns "panic: temporary file seek" kenneho Linux - Software 5 12-23-2008 03:27 AM
Standard commands give "-bash: open: command not found" even in "su -" and "su root" mibo12 Linux - General 4 11-11-2007 10:18 PM
Emulating "rm" with a script mike9287 Linux - Newbie 13 07-06-2006 09:50 AM


All times are GMT -5. The time now is 03:17 AM.

Main Menu
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