LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Software (https://www.linuxquestions.org/questions/linux-software-2/)
-   -   emulating "rm " command (https://www.linuxquestions.org/questions/linux-software-2/emulating-rm-command-790009/)

jaikris 02-18-2010 02:07 PM

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.

dadrunamok 02-18-2010 02:16 PM

Why not use cp or mv? They already do that job (cp copies files and mv moves them).

tlgrok 02-18-2010 02:25 PM

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

http://www.linuxquestions.org/questi...4/#post1205474

jaikris 02-19-2010 10:00 PM

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

jaikris 02-19-2010 11:41 PM

sorry for the inconvenience caused .. can any one guide me on this ...

John VV 02-19-2010 11:44 PM

have you looked through Gnome?
it auto puts stuff in the "trash"

hack that code , why reinvent the wheel

catkin 02-20-2010 01:49 AM

Quote:

Originally Posted by John VV (Post 3870067)
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!

jaikris 02-21-2010 10:01 PM

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

chrism01 02-22-2010 01:05 AM

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?

micxz 02-22-2010 01:17 AM

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

i92guboj 02-22-2010 03:50 AM

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.

jaikris 05-30-2010 10:50 PM

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

Nylex 05-31-2010 12:09 AM

Quote:

Originally Posted by jaikris (Post 3986939)
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?

jaikris 05-31-2010 12:50 AM

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 ..

micxz 05-31-2010 01:41 AM

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'

unSpawn 05-31-2010 04:58 PM

What you should do is put rm back to where it was and forget using aliases or scripts like yours. Aliases can evaded, don't cover applications removing files and scripts can behave unexpectedly. besides replacing a system binary with a kludge isn't standards compliant anyway.

Re-inventing the wheel was done before, see: http://directory.fsf.org/project/trashcan/, http://sourceforge.net/projects/rmw/, http://code.google.com/p/safe-rm/, http://www.linuxquestions.org/questi...8/#post3564350.

Libtrash intercepts syscalls through LD_PRELOAD. From the README: "libtrash works with any GNU/Linux program, both at the console and under XFree86, and operates independently of the programming language the program was written in. The only exception are statically linked programs, which you probably won't find. It can be extensively configured by each user through a personal, user-specific configuration file." (Also see http://www.linuxquestions.org/questi...9/#post3453347.)

There also is ext3cow (but I don't know how it fares right now) and then there's versioning filesystems.

salasi 06-01-2010 05:11 AM

Quote:

Originally Posted by unSpawn (Post 3987860)
What you should do is put rm back to where it was and forget using aliases or scripts like yours.... besides replacing a system binary with a kludge isn't standards compliant anyway.

I completely agree on that part, but

Quote:

Originally Posted by unSpawn (Post 3987860)
Aliases can evaded, don't cover applications removing files and scripts can behave unexpectedly.

I think that is one case in which something like this (not this, exactly, but something along similar lines) can make sense; if you are concerned about 'finger trouble' with a powerful command like 'rm -Rf' and variants thereof, then aliasing it to something that gives you a second chance can make sense (of course, you can also argue that someone who is not in control of what their fingers do shouldn't be in possession of a computer...not that I would make this argument, you understand).

If, of course, you replace a system binary, that changes things for all users; that seems sub-optimal.

Personally, I won't take any code seriously that is not in code tags or otherwise formatted for easy reading, because that means that the OP hasn't taken the effort to make it easy to read; why should hundreds of people have to make that effort because one didn't do it correctly? But, you'll have got exit codes wrong, everyone does.

So, when you try to update, uninstall or install software, expect the system not to perform the procedure correctly. In some cases, it will only be trivially different from a correct procedure, and the system will still work correctly. In others it won't.

Have Fun!

jaikris 06-08-2010 02:23 PM

am using " tar -cvf <destination > source > /dev/null " for the new command ( rm - emulation )

evan after am redirectig the output message , still am receiving the message : tar: Removing leading `/' from member names

how to avoid the message from screen

cantab 06-22-2010 04:44 AM

Try 2&> /dev/null to redirect stderr as well. Bear in mind you're then sending stuff that might be important - tar telling you it's gone wrong - to the bit bucket.


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