0

Reference: how to change `rm` to as a command like `mv ~/ .trash`

I would like to implement recycle bin utility in UNIX since UNIX not has one.
In .bashrc file I change alias rm='move.sh"

as per above Reference guide I have written function in my move.sh script

Script : move.sh

 #!/bin/sh

TRASH=<trash directory path>

move() { mv "$@" $TRASH;
} move

when I run I am getting below error. Can some one please help what's wrong I am doing?

mv: Insufficient arguments (1) Usage: mv [-f] [-i] f1 f2
mv [-f] [-i] f1 ... fn d1
mv [-f] [-i] d1 d2

Rocky
  • 3
  • 1
    Trash does not work like that. The issue is obvious if you consider trashing files foo/hello.c and bar/hello.c. Either (a) you need to create the directory trees, or (b) you have one hello.c in your trash but you cannot know whether it came from foo or bar. A decent Trash will preserve two or more distinct versions of hello.c trashed at different times, even from the same directory. It needs to store metadata to be able to untrash files, much like an archive. – Paul_Pedant Feb 19 '22 at 22:22
  • Take a look at this: https://github.com/andreafrancia/trash-cli. It's in the main Arch repo (as trash-cli), not sure about other distros. – ajgringo619 Feb 20 '22 at 01:47
  • While my answer addressed "What's wrong with the code?", this seems to more accurately satisfy "I want a trash function in Linux". I encourage the OP to look into trash-cli once the script is fixed. Writing your own, proper trash script is a bit more work than moving a file to a designated folder. :) – Kaffe Myers Feb 22 '22 at 20:15

1 Answers1

4

First, if that is the entirety of your script, you don't need to first make a function. Simply write:

#!/bin/bash
TRASH=<trash directory path>
echo "$TRASH"
mv "$@" "$TRASH"

But to answer what is wrong with your script, the most obvious error is that you are not passing any arguments to your function. In other words, the last line in your script should also get the script arguments passed to it. move should become move "$@".

Other than that, remember to always quote your variables if you don't have an explicit reason not to.


As you seem to want this in .bashrc, just create the function straight in there:

cat <<'EOF' | tee -a "$HOME/.bashrc"
trash(){ local t="/my/trash/path"; [[ -n "$*" ]] && echo "$t" && mv "$@" "$t" ;}
EOF

I strongly suggest not over-writing the standard rm command, which is why I made it trash in my example

  • Actually my preferred method usually, but I find "no output" to confuse the less experienced, so picked tee this time. Same result regardless. – Kaffe Myers Feb 19 '22 at 23:00
  • 1
    +1. Also... RE: quoting variables unless you have a specific reason not to, the same applies to script args like $1, $2, $@, and $. Parameter expansion is prone to the same kinds of errors and problems as variable expansion. Your trash function should quote $ too, otherwise it will break if there's more than one arg - e.g. [[ -n "$*" ]]. BTW, to demonstrate: run [[ -n 1 2 3 ]] && echo true in a bash terminal. Note the errors. Then run [[ -n "1 2 3" ]] && echo true. -n only takes one arg. – cas Feb 20 '22 at 09:07
  • 1
    BTW, this would be worth another +1 by itself, if it were possible: I also agree on not over-writing standard rm, certainly not without writing a complex function capable of handling all of rm's options, and probably not even then. A separate trash function is better. – cas Feb 20 '22 at 09:11
  • for your first comment, it has been edited! – Kaffe Myers Feb 20 '22 at 09:21
  • @KaffeMyers - as per you suggested while calling move function added move "$@" solve the issue. Thanks you for your help! – Rocky Feb 21 '22 at 22:31