Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

Instead of using the "RM" command for things. I would like to have a custom command that would move the file to a "trash" folder with the dir it had. for example.

If a file was /home/test/folder1/xx/file.so when it was processed with the command, it would move to /trash/home/test/folder1/xx/file.so that way I have no conflicts with something with that name already being in the trash.

Anyone have any ideas how to implement this and also allow it to be used recursively so directories can be trashed too?

share|improve this question

migrated from stackoverflow.com Jan 19 '10 at 3:54

This question came from our site for professional and enthusiast programmers.

3 Answers 3

Have a look at trash-cli

share|improve this answer
    
been using this, and i love it. –  George IV Jan 18 '10 at 5:04
    
This is what I needed. I prefer to have my own directory and do things my way but this will do what I want. –  Unknown Jan 18 '10 at 5:06
    
And you can always create an alias to rm, so it will use trash-cli instead of rm. Just put alias rm='trash-put' in your ~/.bashrc and any new shell should use trash when you call rm. If you want to use the real rm just type \rm and it will work. –  Flávio Amieiro Jan 18 '10 at 18:12
5  
I would strongly advise against aliasing rm. You'll be using it happily, safe in the knowledge that you have the trash files in case you get something wrong, and one day you'll find you don't. It might be that you're on a different machine, or that somehow your .bashrc has been overwritten. Much better to have your own command, find it doesn't work and go fix it than permanently delete files by mistake. –  Paul Rayner Jan 19 '10 at 3:18

This thread may be helpful to you.

share|improve this answer

I use:

  function trash () { # safe rm to trash
  local path
  for path in "$@"; do
    # ignore any arguments
    if [[ "$path" = -* ]]; then :
    else
      local dst=${path##*/}
      # append the time if necessary
      while [ -e ~/.Trash/"$dst" ]; do
        dst="$dst "$(date +%H-%M-%S)
      done
      mv "$path" ~/.Trash/"$dst"
    fi
  done
}

Safe, and easy.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.