Advertisement
pablokal

del script

Oct 7th, 2011
556
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 3.75 KB | None | 0 0
  1. #!/bin/bash
  2. #
  3. # del   A send-to-trash command for Gnome users.
  4. #   Interactive overwrite and rename.
  5. #   Volume aware. (Trash directory preferences can be set in the
  6. #   settrash function).
  7. #       Modified for Arch users by pablokal
  8. # Licence: GNU GPL
  9. # Version:      1.1  12-Feb-2006  Filipe Agapito (h314to.AT.gmail.com)
  10. #       Added options, quiet mode and file count.
  11.  
  12.  
  13. # findmp:   finds the mount point for the device where this    
  14. #       file is located.                   
  15. findmp () {
  16. found=false
  17. mp=""
  18. dir="`pwd`"
  19. until [ "$found" = "true" ] ; do
  20.     # Grep for rw filesystems in /etc/mtab
  21.     for i in `grep rw /etc/mtab  | awk '{print $2}'` ; do
  22.         if [ "$i" = "$dir" ] ; then #This is the  mountpoint
  23.             mp="$dir"
  24.             found=true
  25.             break
  26.         fi
  27.     done
  28.     # Go up one dir
  29.     cd ..
  30.     dir="`pwd`"
  31. done
  32. }
  33.  
  34. # settrash: sets the trash dir for the file            
  35. settrash () {
  36.     case "$mp" in
  37.         /home)
  38.         trash=$HOME/.local/share/Trash/files
  39.         ;;
  40.         /)
  41.         trash=$HOME/.local/share/Trash/files
  42.         ;;
  43.         # Here you can create your own options for the $trash variable
  44.         #directory)
  45.         #trash=
  46.         #;;
  47.         *)
  48.         trash="$mp"/.Trash-$UID/files
  49.         ;;
  50.     esac
  51. }
  52.  
  53. # delete:   checks if file allready exists in trash and deletes
  54. delete () {
  55.     # Create trash dir if it doesn't exits
  56.     if [ ! -e "$trash" ] ; then
  57.         mkdir "$trash"
  58.     fi
  59.     if [ -d "$trash"/"$file" ] && [ ! -d "$file" ] ; then
  60.         echo "File ""$trash"/"$file"" exists and is a directory."
  61.         # We can't move. Must rename.
  62.         ren_delete
  63.     elif [ -e "$trash"/"$file" ] ; then
  64.         echo "File ""$trash"/"$file"" exists."
  65.         overwrite
  66.     else
  67.         if [ "$verbose" = "yes" ] ; then
  68.             mv -v -- "$file" "$trash"
  69.         else
  70.             mv -- "$file" "$trash" 
  71.         fi
  72.     fi
  73. }
  74.    
  75. # overwrite:    Interactive overwrite                  
  76. overwrite () {
  77.     ow=""
  78.     echo "Overwrite?(y/n)"
  79.     read ow
  80.     case $ow in
  81.         y)
  82.         if [ "$verbose" = "yes" ] ; then
  83.             mv -v -- "$file" "$trash"
  84.         else
  85.             mv -- "$file" "$trash" 
  86.         fi
  87.         ;;
  88.         n)
  89.         ren_delete "$file"
  90.         ;;
  91.         *)
  92.         echo "del: Invalid option: "$ow"."
  93.         echo "del: "$file" will not be deleted."
  94.         ;;
  95.     esac
  96. }
  97.  
  98. # ren_delete:   Interactive rename and delete              
  99. ren_delete () {
  100.     rd=n
  101.     new=""
  102.     echo "Rename and delete? (y/n)"
  103.     read rd
  104.     case $rd in
  105.         y)
  106.         echo "Enter name:"
  107.         read new
  108.         if [ "$verbose" = "yes" ] ; then
  109.             mv -v -- "$file" "$trash"/"$new"
  110.         else
  111.             mv -- "$file" "$trash"/"$new"
  112.         fi
  113.         ;;
  114.         n)
  115.         echo "del: "$file" will not be deleted."
  116.         ;;
  117.         *)
  118.         echo "del: Invalid option: "$rd"."
  119.         echo "del: "$file" will not be deleted."
  120.         ;;
  121.     esac
  122.  
  123. }
  124. # Command line options
  125. options () {
  126. case $1 in
  127.     -v)
  128.     verbose=yes
  129.     ;;
  130.     --verbose)
  131.     verbose=yes
  132.     ;;
  133.     -h)
  134.     printhelp
  135.     ;;
  136.     --help)
  137.     printhelp
  138.     ;;
  139.     *)
  140.     verbose=no
  141.     ;;
  142. esac
  143. }
  144.  
  145. printhelp () {
  146. echo
  147. echo "del   A send-to-trash command for Gnome users."
  148. echo
  149. echo "Version: 1.1  12-Feb-2006  Filipe Agapito (h314to.AT.gmail.com)"
  150. echo
  151. echo "Usage:"
  152. echo "      del [option] file1 file2 ..."
  153. echo
  154. echo "Options:"
  155. echo
  156. echo "  -v, --verbose   explain what is being done"
  157. echo "  -h, --help  display this help and exit"
  158. echo " "
  159. echo "The filename may start with a - unless it's named like one"
  160. echo "of the options above."
  161. echo
  162. }
  163.  
  164. calldir="`pwd`"
  165. options "$1"
  166. n=0
  167. printnum=yes
  168. for name in "$@" ; do
  169.     if [ "$name" = "-v" ] || [ "$name" = "--verbose" ] ; then
  170.         continue   
  171.     elif [ "$name" = "-h" ] || [ "$name" = "--help" ] ; then
  172.         printnum=no
  173.         break  
  174.     elif [ ! -e "$name" ] ; then
  175.         echo "del: File "$name" doesn't exist."
  176.     else
  177.         cd "$calldir"
  178.         cd "`dirname "$name"`"
  179.         wdir="`pwd`"
  180.         file="`basename "$name"`"
  181.         findmp
  182.         settrash
  183.         cd "$wdir"
  184.         delete
  185.         cd $calldir
  186.         n=`expr $n + 1`
  187.     fi
  188. done
  189. if [ "$printnum" = "yes" ] ; then
  190.     if [ $n -eq 1 ] ; then
  191.         echo "del: $n file sent to trash"
  192.     else
  193.         echo "del: $n files sent to trash"
  194.     fi
  195. fi
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement