Put this in your .bashrc
or .zshrc
:
function abspath() {
if [ -d "$1" ]; then
# dir
(cd "$1"; pwd)
elif [ -f "$1" ]; then
# file
if [[ $1 = /* ]]; then
echo "$1"
elif [[ $1 == */* ]]; then
echo "$(cd "${1%/*}"; pwd)/${1##*/}"
else
echo "$(pwd)/$1"
fi
fi
}
TRASH_DIR="${HOME}/.trash"
function trash() {
n="$(date +%Y-%m-%d_%H-%M-%S)"
for f in "$@"; do
a="$(dirname $(abspath $f))"
t="${TRASH_DIR}/${n}/${a}"
mkdir -p "${t}"
mv "$f" ${t}
done
}
function trash_empty() {
rm -rf "${TRASH_DIR}"
mkdir -p "${TRASH_DIR}"
}
The trash function moves each argument and preserve the directory structure to ${TRASH_DIR}/DATE
.
abspath
from https://stackoverflow.com/a/23002317/2627873.