General TC > Programming & Scripting - Unofficial

trash

(1/2) > >>

jpeters:
Moves file/folder to trash directory with $1 (eg, "trash myfile").  Without $1 (eg, "trash"), prompts
for deleting.  Edit PATH (script will create/remove directory).  

For use with DFM, create icon. Set shellcommand to "aterm -e !0!".  Drag files to icon (move), or click for viewing/deleting.


--- Code: ---#!/bin/ash


#### trash.sh script, jeters
###  deletes $1 files/folders; without $1, prompts to view/delete

## Edit PATH:
TRASH="/mnt/hda3/tmp/trash"

. /etc/init.d/tc-functions

[ -d /mnt/hda3/tmp/trash ] || sudo mkdir -p ${TRASH}

if [ $1 ]; then
  sudo mv "$@" ${TRASH}
else
  if [ "$(ls ${TRASH})" ]; then
     clear
     ls ${TRASH}
     echo "_____________"
     echo ${GREEN}"delete? (y)"${NORMAL}
     read RESPONSE
       case ${RESPONSE} in
         "y" )  sudo rm -R ${TRASH} ; echo ${RED}"deleted!" ${NORMAL}; sleep 1 ;;
         "*" )  break ;;
       esac
   else
     echo ${GREEN}"Empty" ${NORMAL}; sleep 1
   fi

fi

exit 0
      

--- End code ---
     
      

jpeters:
screenshot with DFM

[removed due to policy]apps/trash.png

jpeters:
Updated to more user friendly version.

jpeters:
updated to include multiple arguments: eg, "trash file1 file2 mydir";  also "trash myfiles*"

mikshaw:

--- Code: ---  while [ $# -ge 1 ];do
     sudo mv ${1} ${TRASH}
     shift
   done
--- End code ---
Could this be done in just one command rather than a loop?  The mv command supports mulitple files.

--- Code: ---sudo mv "$@" ${TRASH}
--- End code ---

The directory test is using a specific path rather than $TRASH

The delete confirmation could be improved.  If you just press enter when the "y" is visible, you get an error.  It is common to have a default selection displayed in uppercase -- Delete? (y|N) -- which allows the user to just press enter to accept the default.  The error can be avoided either by first testing for the existence of $RESPONSE before comparing it to "y", or by using case instead of if.

Personally I wouldn't delete the trash directory, only its contents...but I can see reasons for doing that.

Navigation

[0] Message Index

[#] Next page

Go to full version