2

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?

2 Answers 2

2

Have a look at trash-cli. It provides a command line interface to the same "trash can" used by KDE, GNOME, and XFCE.

5
  • been using this, and i love it.
    – George IV
    Jan 18, 2010 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, 2010 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. Jan 18, 2010 at 18:12
  • 6
    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, 2010 at 3:18
  • For the record, trash-cli moved to GitHub github.com/andreafrancia/trash-cli but I can't edit the answer.
    – Calimo
    Apr 12, 2016 at 14:29
0

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.

1
  • Not very safe, this is for instance guaranteed to cause lots of issues if the file you're trying to delete is on a partition other than your home
    – Calimo
    Apr 12, 2016 at 14:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.