trash-cli icon indicating copy to clipboard operation
trash-cli copied to clipboard

More safety Tips / Suggestions

Open amishmm opened this issue 11 years ago • 2 comments

This is not really a bug but a TIP. (zsh related). I do not know where else can I put it.

Just sharing a few line code (zsh only) which others may be in search for. (because I searched too and finally made one on my own)

Purpose:

  1. trash / trash-put should ask before trashing, just like rm -i
  2. trash-put should not trash directory but just a regular file
  3. trash-empty and trash-rm should ask first. Infact ask twice, because its going to be erased forever! (Intention is to be 100% sure!)
  4. If user uses rm instead of trash-put, ask first before running rm

Here is the code: (put in your .zshrc)

if (( $+commands[trash-put] )); then
  rm() {
    if read -q "?$0: are you sure you do NOT want to use trash-put? [N/y] "; then
        echo ""
        nocorrect command rm -i "$@"
    fi
  }
  trash-put() {
    local file;
    for file in "$@"; do
      if [[ ! -f $file ]]; then
          echo "$0: cannot remove '$file': not a regular file"
          continue
      fi
      if read -q "?$0: trash regular file '$file'? [N/y] "; then
        echo ""
        nocorrect command trash-put -v -- $file
      fi
    done
  }
  alias trash=trash-put
fi
if (( $+commands[trash-rm] )); then
  trash-rm() {
    if read -q "?$0: are you sure you want to use trash-rm? [N/y] "; then
      echo ""
      if read -q "?$0: are you REALLY sure you want to use trash-rm? [N/y] "; then
          echo ""
          nocorrect command trash-rm "$@"
      fi
    fi
  }
fi
if (( $+commands[trash-empty] )); then
  trash-empty() {
    if read -q "?$0: are you sure you want to empty trash? [N/y] "; then
      echo ""
      if read -q "?$0: are you REALLY sure you want to empty trash? [N/y] "; then
          echo ""
          nocorrect command trash-empty "$@"
      fi
    fi
  }
fi

Hope it helps!

amishmm avatar Mar 19 '14 06:03 amishmm

Related to https://github.com/andreafrancia/trash-cli/issues/10

andreafrancia avatar May 12 '21 20:05 andreafrancia

From the 0.21.5.25 release trash-put honors the -i option. Example:

$ touch foo
$ trash-put -i foo
trash-put: trash regular empty file 'foo'? y
$ 

You can add alias trash-put='trash-put -i' to your .bashrc

andreafrancia avatar May 25 '21 18:05 andreafrancia