Find the answer to your Linux question:
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 ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined! ultimatelinux's Avatar
    Join Date
    Mar 2010
    Posts
    36

    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.

  2. #2
    Linux User
    Join Date
    Nov 2009
    Location
    France
    Posts
    292
    You can also alias your script so that the rm command remains usable safely :

    Code:
    alias rm='rem'
    It is certainly a good practice for 98% of users. Secured files should be shredded some other way however.
    0 + 1 = 1 != 2 <> 3 != 4 ...
    Until the camel can pass though the eye of the needle.

  3. #3
    Linux Guru Cabhan's Avatar
    Join Date
    Jan 2005
    Location
    Seattle, WA, USA
    Posts
    3,252
    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:
    Code:
    bricka@eagle ~/test/bash $ rem foo
    foo
    bricka@eagle ~/test/bash $ rem "foo bar"
    foo
    bar
    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:
     function rem()
    {
        for i in "$@"
        do
            mv $i $TRASHPATH/$i
        done
    }
    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:
    bricka@eagle ~/test/bash $ rem foo
    foo
    bricka@eagle ~/test/bash $ rem "foo bar"
    foo bar

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •