This is the old SliTaz forum - Please use the main forum.slitaz.org

[Utility][Shell script] Trash can
  • babaorumbabaorum December 2009
    Advertising:
    Oh yeah the absolute Newbie Feature is now available on SlITaz. You are vowed to Windows but cannot admit it and try to be cool on Linux ? But you are afraid of typing every single command line because you know Daddy will break your neck if you break the Comp`Beast that he bought to himself last Christmas and did *not* allow you to tweak with EvilNux ?

    Thou are saved, Gentleman of The Newbish Attitude: trash-can is nice, trash-can will save your soul and keep your neck as nice as when you mother looked at you for the very first time, her eyes full of tears of happiness.


    In clear: whatever your push in trash-can is kept until you clean it, no twisted use.
    Just think to use trash your-trash-to-get-rid-of rather than rm [-f] your-trash-to-get-rid-of

    Trash-can will allow you:

    - to get an eye on its content, regardless of knowing where is the trash-can directory,

    - clean it,

    - keep a log of trashed files/directory (if directory: not of its own contents),

    - clean this log,

    - output the trash-can or the logfile path

    Trash-can will probably allow you:

    - to save Amazonian forest,

    - to build a better world for your children.

    Can you still hesitate ? Adopt Trash-Can, it is Magic, it is Safe, it obeys better than a dog.

    (source code in following message)

    -- Babaorum, Da UnMighty
  • babaorumbabaorum December 2009
    Source code, part 1/2:

    #!/bin/sh
    # trash - virtual trash-can to avoid accidental removing of files

    WORKDIR="$HOME/.trash"
    LOGFILE="$HOME/.trash.log"
    ERR_NOGOODARG="\"$1\" is not an existing regular file or directory and not a known parameter."
    ERR_NOWORKDIR="Script cannot work without a trash-directory (should be: \"$WORKDIR\")."
    ERR_NOLOG="Logfile doesn't exist. It may not have been created yet (first use)."
    SCRIPTBASENAME=`basename "$0"`

    help()
    {
    echo "Usage : $SCRIPTBASENAME file-or-directory : drops file/directory to trash-can"
    echo " $SCRIPTBASENAME -h|--help : displays this help screen"
    echo " $SCRIPTBASENAME -s|--size : displays the size of the trash-can"
    echo " $SCRIPTBASENAME -e|--empty : gets definitively rid of trash-can content"
    echo " $SCRIPTBASENAME -l|--log : displays logfile"
    echo " $SCRIPTBASENAME -tp|--trash-path : displays path of trash-can"
    echo " $SCRIPTBASENAME -lp|--log-path : displays path of logfile"
    echo ""
    echo "(Your logfile is: $LOGFILE , your trash-directory is: $WORKDIR )"
    }
    echo_error()
    {
    echo "ERROR ! $1" >&2
    echo "Type \"trash -h\" or \"trash --help\" for usage."
    }
    trash_size()
    {
    [ ! -d "$WORKDIR" ] && echo_error "$ERR_NOWORKDIR" && exit 1
    cd "$WORKDIR"
    echo "Total size of trash-can is `du -ms | sed 's/[ \.\t]//g'` Mb."
    }
    log()
    {
    [ -s "$LOGFILE" ] || touch $LOGFILE
    echo "`date` : $@" >> $LOGFILE
    }

  • babaorumbabaorum December 2009
    Source code, part 2/2:

    # Parameters control
    [ -z "$1" -o "$1" = "--help" -o "$1" = "-h" ] && help && exit 0
    [ "$1" = "-s" -o "$1" = "--size" ] && trash_size && exit 0
    [ "$1" = "-tp" -o "$1" = "--trash-path" ] && echo "$WORKDIR" && exit 0
    [ "$1" = "-lp" -o "$1" = "--log-path" ] && echo "$LOGFILE" && exit 0
    if [ "$1" = "-l" -o "$1" = "--log" ]; then
    [ -s "$LOGFILE" ] && cat "$LOGFILE" && exit 0
    echo_error "$ERR_NOLOG" && exit 1
    fi
    if [ "$1" = "-e" -o "$1" = "--empty" ]; then
    echo "CAUTION: you are about to DEFINITIVELY get rid of all trash-can content."
    echo -n ">> Are you really sure ? [y|N] "
    read foo
    if [ "$foo" = "y" -o "$foo" = "Y" ]; then
    rm -fR "$WORKDIR/*"
    echo "Trash-can is now empty."
    log "Trash-can empty"
    echo -n ">> Do you want to erase the logfile too ? [y|N] "
    read foo2
    if [ "$foo2" = "y" -o "$foo2" = "Y" ]; then
    [ ! -s "$LOGFILE" ] && echo_error "ERR_NOLOG" && exit 1
    mv "$LOGFILE" "${LOGFILE}.bak"
    tail -n 1 "${LOGFILE}.bak" > "$LOGFILE"
    mv "${LOGFILE}.bak" "$WORKDIR"
    echo "Logfile has been dropped in trash-can (\"$WORKDIR\")."
    log "Logfile $LOGFILE dropped in"
    else
    exit 1
    fi
    exit 0
    else
    exit 1
    fi
    fi
    [ ! -f "$1" -a ! -d "$1" ] && echo_error "$ERR_NOGOODARG" && exit 1

    # Proposer de créer la corbeille si elle n'existe pas déjà
    if [ ! -d "$WORKDIR" ]; then
    while true
    do
    echo "CAUTION: trash-can \"$WORKDIR\" (directory) is not existing."
    echo -n ">> Should I create it for you ? [Y|n] "
    read foo
    [ "$foo" = "n" -o "$foo" = "N" ] && echo_error "$ERR_NOWORKDIR" && exit 1
    [ "$foo" = "y" -o "$foo" = "Y" ] && break
    done
    mkdir -p "$WORKDIR"
    log "Trash-can created as $WORKDIR"
    fi

    # Get rid of file/dir to trash-can
    cp -a "$1" "$WORKDIR"
    rm -fR "$1"
    echo "\"$1\" has been dropped in trashcan (\"$WORKDIR\")."
    log "$1 dropped in"
    trash_size
    exit 0

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Sign In Apply for Membership

SliTaz Social