Skip to content

Instantly share code, notes, and snippets.

@gmolveau
Last active May 9, 2021 15:03
Show Gist options
  • Save gmolveau/057feb9d46c839dd114cdc71cd4ea75b to your computer and use it in GitHub Desktop.
Save gmolveau/057feb9d46c839dd114cdc71cd4ea75b to your computer and use it in GitHub Desktop.
linux/ubuntu server trash shell function/command

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment