Last active
January 14, 2021 20:14
-
-
Save isaac-ped/6c0fd456c4340aa6d8f4bf82d4686653 to your computer and use it in GitHub Desktop.
Trash, don't remove!
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash -e | |
usage() { | |
echo "Move to trash directory without deleting" | |
echo "Usage: $(basename $0) [-v] thing-to-trash1 [thing-to-trash2 ...]" | |
} | |
VERBOSE=0 | |
POSITIONAL="" | |
while (( "$#" )); do # While there are arguments left | |
case "$1" in | |
-h|--help) | |
usage | |
exit 0 | |
;; | |
-v|--verbose) | |
VERBOSE=1 | |
shift | |
;; | |
--) # End of arguments | |
shift | |
break | |
;; | |
*) # Keep positional | |
# ???: Does this deal with quotes well? | |
POSITIONAL="$POSITIONAL $1" | |
shift | |
;; | |
esac | |
done | |
# Set POSITIONAL back to the argument array | |
eval set -- "$POSITIONAL" | |
if [[ "$#" == 0 ]]; then | |
usage | |
exit 1; | |
fi | |
if git rev-parse --show-toplevel 2> /dev/null; then | |
LABEL="$(basename $GITREPO)" | |
else | |
LABEL="$(realpath . --relative-to $(realpath ../..))" | |
fi | |
TRASHDIR="$HOME/trash/$LABEL/$(date +%F)" | |
echo "Moving to $TRASHDIR" | |
for TOMOVE in "$@"; do | |
FROMDIR=$(dirname $TOMOVE) | |
TODIR="$TRASHDIR/$FROMDIR" | |
mkdir -p $TODIR | |
TOFILE="$TODIR/$(basename $TOMOVE)" | |
NEWED=false | |
while [[ -e $TOFILE ]]; do | |
TOFILE=${TOFILE}.new | |
NEWED=true | |
done | |
if $NEWED ; then | |
echo "Using $TOFILE to avoid conflicts" | |
fi | |
if [[ $VERBOSE = 1 ]]; then | |
echo "Moving '$TOMOVE' to '$TOFILE'" | |
fi | |
mv "$TOMOVE" "$TOFILE" | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment