Skip to content

Latest commit

 

History

History
42 lines (36 loc) · 1.03 KB

README.md

File metadata and controls

42 lines (36 loc) · 1.03 KB

recycle-bin

Simple and Stupid Linux Bash Recycle Bin

del() {
    while [ $# -gt 0 ]; do
        oldpath=$(realpath -s "$1")
        olddir=$(dirname "$oldpath")
        oldname=$(basename "$oldpath")
        newdir=~/.recycle_bin/$(date -I)
        newname=$(date -Ins)-"$oldname"
        mvbksh=$newdir/.mvbk."$newname".sh

        mkdir -p $newdir
        mv "$1" $newdir/"$newname"

        mvbksh_lines=(
            "#!/bin/bash"
            ""
            "set -e"
            ""
            "if [ -e '$olddir/$oldname' ]; then"
            "    echo '\"$olddir/$oldname\" exists!'"
            "    echo 'Fail to move back.'"
            "    exit 1"
            "fi"
            ""
            "mkdir -p '$olddir'"
            "mv '$newdir/$newname' '$olddir/$oldname'"
            "rm '$mvbksh'"
        )
        printf "%s\n" "${mvbksh_lines[@]}" > "$mvbksh"
        chmod u+x "$mvbksh"

        shift
    done
}

License

GLWT(Good Luck With That) Public License