Command Line Trash - Programming On Unix

Users browsing this thread: 2 Guest(s)
seninha
Long time nixers
These are the scripts I use to trash and "untrash" (remove from trash) files.
I set the $TRASH environment variable to $HOME/trash. I do not like the freedesktop convention on using .local/share

trash:
Code:
#!/bin/sh
# trash: move files to trash $TRASH
# this file in public domain

usage() {
    echo "usage: trash file..." >&2
    exit 1
}

# get trash directory to move file $1 into
gettrash() {
    mountfile="$(df "$1" | awk 'END {print $NF}')"
    mounthome="$(df "$HOME" | awk 'END {print $NF}')"

    if [ "$mountfile" != "$mounthome" ]
    then
        echo "$mountfile/.Trash-$UID"
    else
        echo ${TRASH:-$HOME/trash}
    fi

}

# delete file with full path $1 into trash directory $2
trash() {
    basename=$(basename "$1")
    body=${basename%.*}
    ext=${basename##$body}

    i=""
    while [ -e "$2/files/$body$i$ext" ]
    do
        if [ -z "$i" ]
        then
            i=1
        else
            i=$((i + 1))
        fi
    done

    cat > "$2/info/$body$i$ext.trashinfo" <<END
[Trash Info]
Path=$1
DeletionDate=$(date +"%FT%H:%M:%S")
END

    mv "$1" "$2/files/$body$i$ext"
}

[ $# -eq 0 ] && usage

for f in "$@"; do

    [ ! -e $f ] && echo "trash: $f: no such file or directory" >&2 && exit 1

    trashdir="$(gettrash "$f")"
    filename="$(readlink -f "$f")"

    # create trash directory
    if ! mkdir -p "$trashdir/files" "$trashdir/info"
    then
        echo "trash: unable to create $trashdir"
        exit 1
    elif [ "$(ls -ld "$trashdir" | awk 'END {print $1}')" != 'drwx------' ]
    then
        chmod 700 "$trashdir"
    fi

    trash "$filename" "$trashdir"
done

exit 0

untrash:
Code:
#!/bin/sh
# untrash: remove files from trash
# this file in public domain

usage() {
    echo "usage: untrash file..." >&2
    exit 1
}

# get info file of deleted file $2 in path $1
getinfo() {
    echo "$1/../info/$2.trashinfo"
}

# get original name of deleted file $2 on info file $1
getorigfile() {
    echo "$(cat $1 | awk -F= '/Path=/ {print $NF}')"
}

[ $# -eq 0 ] && usage

for f in "$@"; do
    if [ ! -e "$f" ]
    then
        echo "trash: $f: no such file or directory" >&2
        exit 1
    fi

    filename="$(readlink -f "$f")"
    basename="$(basename "$f")"

    # get directory of deleted files
    filesdir="$(dirname "$filename")"
    if [ "$(basename $filesdir)" != "files" ]
    then
        echo "trash: $basename: file is not in trash" >&2
        exit 1
    fi

    # get file containing information on deleted file
    infofile="$(getinfo "$filesdir" "$basename")"
    if [ ! -f "$infofile" ]
    then
        echo "trash: $basename: cannot find original directory" >&2
        exit 1
    fi

    # get original name of deleted file
    origfile="$(getorigfile "$infofile" "$basename")"
    if [ -e "$origfile" ]
    then
        echo "trash: $basename: file already exists on original directory" >&2
        exit 1
    fi

    # move deleted file to its original place
    if ! mv "$filename" "$origfile"
    then
        echo "trash: $basename: cannot move file to its original directory" >&2
        exit 1
    fi

    # remove deleted file's information file
    if ! rm "$infofile"
    then
        echo "trash: $basename: cannot clean file from trash" >&2
        exit 1
    fi
done

exit 0


Messages In This Thread
Command Line Trash - by venam - 07-03-2019, 04:02 AM
RE: Command Line Trash - by jkl - 07-03-2019, 06:05 AM
RE: Command Line Trash - by z3bra - 08-03-2019, 04:35 AM
RE: Command Line Trash - by venam - 08-03-2019, 05:38 AM
RE: Command Line Trash - by z3bra - 09-03-2019, 10:04 AM
RE: Command Line Trash - by jkl - 09-03-2019, 10:27 AM
RE: Command Line Trash - by seninha - 14-04-2020, 06:33 PM