Results 1 to 3 of 3
i've seen many users deleting files and reporting how to recover them.
instead of recovering i've thought of redirecting the files deleted to trash.so,that it might be helpful to recover ...
- 03-24-2010 #1
alternative to `rm` command
i've seen many users deleting files and reporting how to recover them.
instead of recovering i've thought of redirecting the files deleted to trash.so,that it might be helpful to recover them.
here's how i do it.
1.take a variable,for example:TRASHPATH.
2.assign the exact path of trash to that variable.
most likely,trash path will be /home/username/.Trash.
3.here's the code just paste this into your .bashrc file.
##################code starts here
TRASHPATH=/home/username/.Trash(or)/home/username/.local/share/Trash
function rem()
{
for i in $*
do
mv $i $TRASHPATH/$i
done
}
##################code ends here
now,if you want to delete files just issue the command
rem file1
you can give multiple files also
rem file1 file2 file3 file4 file5 etc.
p.s:instead of trash,you can direct to another folder also
don't include a slash(/) at the end of path in TRASHPATH variable
for most of the disto's path of trash is ~/.Trash.
in some distro's it is /home/username/.local/share/Trash
hope you all like this.
waiting for your feedback.
- 03-24-2010 #2Linux User
- Join Date
- Nov 2009
- Location
- France
- Posts
- 292
You can also alias your script so that the rm command remains usable safely :
It is certainly a good practice for 98% of users. Secured files should be shredded some other way however.Code:alias rm='rem'
0 + 1 = 1 != 2 <> 3 != 4 ...
Until the camel can pass though the eye of the needle.
- 03-24-2010 #3
Well, for securely deleting files, there is the aptly-named "shred" command
.
As for the above trashing script, it makes a common mistake that people make in Bash scripting, which is that it doesn't handle spaces. To test this, I modified the function so that it would simply print the name of the file that it would delete instead of actually moving it to the trash:
We see that instead of printing "foo bar" as a single file, it gets treated as two files called "foo" and "bar". This is because of how $* is handled in the for loop. The more correct way to do this would have been to use $@, and quote it:Code:bricka@eagle ~/test/bash $ rem foo foo bricka@eagle ~/test/bash $ rem "foo bar" foo bar
Note that I've changed your $* to "$@" (note the quotes). $@ and $* behave basically the same, except that when quoted, $@ expands to separate words, as opposed to $*, which expands to a single word when quoted. Now the function behaves correctly:Code:function rem() { for i in "$@" do mv $i $TRASHPATH/$i done }
Code:bricka@eagle ~/test/bash $ rem foo foo bricka@eagle ~/test/bash $ rem "foo bar" foo bar
DISTRO=Arch
Registered Linux User #388732


Reply With Quote