Page 1 of 3 123 LastLast
Results 1 to 10 of 21

Thread: A send-to-trash command for the shell

  1. #1
    Join Date
    Nov 2005
    Location
    Lisbon, Portugal
    Beans
    74
    Distro
    Ubuntu 8.10 Intrepid Ibex

    Lightbulb A send-to-trash command for the shell. NEW:Volume-aware "del" attached.

    First of all, I'm sorry for posting a howto with such a lame and easily replaceable one line shell script, but I think it will help keep the newbies and the distracted and/or lazy (like me! ) a bit safer.

    Have you ever done a deleted a file in in the shell, only to find out 3 seconds latter that you made a mistake? This is what I do to keep that kind of trouble away from me:

    1. Open you favourite editor and create a file called "del" (or whatever you wish to call it, but i'll use del in this exemple). Let's say it's gedit:

    Code:
    gedit ~/bin/del
    2. Write this there:

    Code:
    #!/bin/bash
    mv -vi -- "$@" $HOME/.Trash
    You can see this in the manpage, but anyway:
    v =verbose (i.e. explain what is being done)
    i =interactive ( prompts you before overwriting)
    -- = so you won't get in trouble with names that start with a -
    "$@" = everything in front of the command (which in this case is our "del" shell script)

    3. Save it.

    4. Now let change the permissions of the file, so that you can execute it.

    Code:
    chmod -v 755 ~/bin/del
    5. And that's it. Now, you can do things like:

    Code:
     del file1 file2 directory1/  directory2/ -strage_filename
    and it all gets send to the Trash (a.k.a. Recycle Bin).


    Note: I assume you have a "bin" dir in you home directory. If you don't you have two options:
    1. Create it...
    Code:
     mkdir ~/bin
    and add it to your PATH variable...
    Code:
     echo "export PATH=$HOME/bin:$PATH" >> ~/.bashrc
    You must use two > signs ( i.e. >> like above) or else you will overwrite your .bashrc file.
    Note that you must restart the terminal, reload the env variables
    Code:
    . ~/.bashrc
    or do
    Code:
     export PATH=$HOME/bin:$PATH
    for the ~/bin directory to be in you PATH variable.

    2. Save your file to /usr/local/bin or /urs/bin modifying the howto accordingly.


    You need more?
    I made a "volume-aware" version of del, which autodetects the volume's Trash directory and deletes to there. It has interactive overwrite and rename, command line options and can be custumized to override the volume default trash directory. I'm attaching it to this howto. You just have to download
    it, extract it (tar xvzf del.tar.gz) and move it to ~/bin, /usr/bin or /usr/local/bin.
    For some help do
    Code:
    del -h
    I did a great deal of testing but, since it's a relatively large script it may still have some bugs. I don't expect them to be dangerous because only "mv" command are used, but consider yourself warned.

    Any feedback, good or bad, and/or sugestions are highly apreciated.
    I'm not explaining it step-by-step here, but I'll answer any questions about it.

    Enjoy.
    Attached Files Attached Files
    Last edited by agapito; February 12th, 2006 at 06:48 PM.

  2. #2
    Join Date
    Jul 2005
    Beans
    Hidden!
    Distro
    Ubuntu

    Re: A send-to-trash command for the shell

    First of all, I'm sorry for posting a howto with such a lame and easily replaceable one line shell script...
    No apologies necessary. Let's face it, it's easier to remember, "del <filename>" than it is to remember, "mv -vi -- <filename> ~/.Trash" any day! Great little oneliner in my opinion! Sometimes simple solutions work better than complex ones anyway. Thanks!
    Rob

  3. #3
    Join Date
    Oct 2005
    Location
    Estonia / Sweden
    Beans
    2,332
    Distro
    Ubuntu Development Release

    Re: A send-to-trash command for the shell

    yeah.... good how-to... i, for one, delete often files that might be needed afterwards... ... i guess your solution will prevent a lot of folish things to happen in the future... thanks

  4. #4
    Join Date
    Nov 2005
    Location
    Lisbon, Portugal
    Beans
    74
    Distro
    Ubuntu 8.10 Intrepid Ibex

    Re: A send-to-trash command for the shell

    Thank you both for your feedback.

  5. #5
    Join Date
    Aug 2005
    Location
    Berkley / Marquette, MI
    Beans
    71
    Distro
    Ubuntu Jaunty Jackalope (testing)

    Re: A send-to-trash command for the shell

    This would be really good to use when installing ubuntu for someone who's computer illiterate. Maybe even put in an alias as "rm"
    Last edited by NMUrugbysteve; February 8th, 2006 at 06:35 PM.

  6. #6
    Join Date
    Feb 2006
    Beans
    31

    Re: A send-to-trash command for the shell

    Thanks!

  7. #7
    Join Date
    Nov 2005
    Location
    Montreal, Canada
    Beans
    525

    Re: A send-to-trash command for the shell

    hehehe. Fun!

    Uhm, just one comment. You suggested creating del in ~/bin:
    Code:
    gedit ~/bin/del
    I came across these two problems as a newbie.
    1. The ~/bin folder doesn't naturally exist. I didn't realise this and initial attempts to save the file were fruitless. Once created del was easily saved.
    2. del didn't work. But that makes sense as it's not in any path the system uses. When transferred to the system's /bin folder it worked perfectly.
    Suggested replacement code:
    Code:
    sudo gedit /bin/del
    Last edited by BobSongs; February 8th, 2006 at 06:16 PM.

  8. #8
    Join Date
    Feb 2005
    Beans
    Hidden!

    Re: A send-to-trash command for the shell

    Code:
    #!/bin/bash
    mv -vi -- "$@" $HOME/.Trash
    I am very against this howto. Say I'm browsing another hard drive and I run that command to delete a file. You know what It will do? It will have to move by file (possibly a big +300mb file) over to where my $HOME partition is. That's horrendous. For this to be actually good, a more advanced script would be needed. One that copied the file to a .Trash of that particular drive. Not all of us have just one partition.
    Sylvia: Look at what you've done to him!
    Christof: I have given Truman the chance to lead a normal life. The world, the place you live in, is the sick place.

  9. #9
    Join Date
    Dec 2005
    Location
    Lund, Skåne
    Beans
    580

    Re: A send-to-trash command for the shell

    Quote Originally Posted by BobSongs
    Suggested replacement code:
    Code:
    sudo gedit /bin/del
    No, put it in /usr/local/bin/

    I write Kupfer, a smart, quick launcher.
    Recommended apps and tools: Vim!

  10. #10
    Join Date
    Nov 2005
    Location
    Where do I live
    Beans
    327

    Re: A send-to-trash command for the shell

    Worked great, guys, thanks!

Page 1 of 3 123 LastLast

Bookmarks

Posting Permissions

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