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. |
Why not use cp or mv? They already do that job (cp copies files and mv moves them).
|
Here is an emulated recyle bin with a few issues, which you could use as a start:
http://www.linuxquestions.org/questi...4/#post1205474 |
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 |
sorry for the inconvenience caused .. can any one guide me on this ...
|
have you looked through Gnome?
it auto puts stuff in the "trash" hack that code , why reinvent the wheel |
Quote:
|
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 |
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:
|
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 |
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. |
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 |
Quote:
|
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 ..
|
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' |
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. |
Quote:
Quote:
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! |
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 |
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. |