Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/bin/bash
- #
- # del A send-to-trash command for Gnome users.
- # Interactive overwrite and rename.
- # Volume aware. (Trash directory preferences can be set in the
- # settrash function).
- # Modified for Arch users by pablokal
- # Licence: GNU GPL
- # Version: 1.1 12-Feb-2006 Filipe Agapito (h314to.AT.gmail.com)
- # Added options, quiet mode and file count.
- # findmp: finds the mount point for the device where this
- # file is located.
- findmp () {
- found=false
- mp=""
- dir="`pwd`"
- until [ "$found" = "true" ] ; do
- # Grep for rw filesystems in /etc/mtab
- for i in `grep rw /etc/mtab | awk '{print $2}'` ; do
- if [ "$i" = "$dir" ] ; then #This is the mountpoint
- mp="$dir"
- found=true
- break
- fi
- done
- # Go up one dir
- cd ..
- dir="`pwd`"
- done
- }
- # settrash: sets the trash dir for the file
- settrash () {
- case "$mp" in
- /home)
- trash=$HOME/.local/share/Trash/files
- ;;
- /)
- trash=$HOME/.local/share/Trash/files
- ;;
- # Here you can create your own options for the $trash variable
- #directory)
- #trash=
- #;;
- *)
- trash="$mp"/.Trash-$UID/files
- ;;
- esac
- }
- # delete: checks if file allready exists in trash and deletes
- delete () {
- # Create trash dir if it doesn't exits
- if [ ! -e "$trash" ] ; then
- mkdir "$trash"
- fi
- if [ -d "$trash"/"$file" ] && [ ! -d "$file" ] ; then
- echo "File ""$trash"/"$file"" exists and is a directory."
- # We can't move. Must rename.
- ren_delete
- elif [ -e "$trash"/"$file" ] ; then
- echo "File ""$trash"/"$file"" exists."
- overwrite
- else
- if [ "$verbose" = "yes" ] ; then
- mv -v -- "$file" "$trash"
- else
- mv -- "$file" "$trash"
- fi
- fi
- }
- # overwrite: Interactive overwrite
- overwrite () {
- ow=""
- echo "Overwrite?(y/n)"
- read ow
- case $ow in
- y)
- if [ "$verbose" = "yes" ] ; then
- mv -v -- "$file" "$trash"
- else
- mv -- "$file" "$trash"
- fi
- ;;
- n)
- ren_delete "$file"
- ;;
- *)
- echo "del: Invalid option: "$ow"."
- echo "del: "$file" will not be deleted."
- ;;
- esac
- }
- # ren_delete: Interactive rename and delete
- ren_delete () {
- rd=n
- new=""
- echo "Rename and delete? (y/n)"
- read rd
- case $rd in
- y)
- echo "Enter name:"
- read new
- if [ "$verbose" = "yes" ] ; then
- mv -v -- "$file" "$trash"/"$new"
- else
- mv -- "$file" "$trash"/"$new"
- fi
- ;;
- n)
- echo "del: "$file" will not be deleted."
- ;;
- *)
- echo "del: Invalid option: "$rd"."
- echo "del: "$file" will not be deleted."
- ;;
- esac
- }
- # Command line options
- options () {
- case $1 in
- -v)
- verbose=yes
- ;;
- --verbose)
- verbose=yes
- ;;
- -h)
- printhelp
- ;;
- --help)
- printhelp
- ;;
- *)
- verbose=no
- ;;
- esac
- }
- printhelp () {
- echo
- echo "del A send-to-trash command for Gnome users."
- echo
- echo "Version: 1.1 12-Feb-2006 Filipe Agapito (h314to.AT.gmail.com)"
- echo
- echo "Usage:"
- echo " del [option] file1 file2 ..."
- echo
- echo "Options:"
- echo
- echo " -v, --verbose explain what is being done"
- echo " -h, --help display this help and exit"
- echo " "
- echo "The filename may start with a - unless it's named like one"
- echo "of the options above."
- echo
- }
- calldir="`pwd`"
- options "$1"
- n=0
- printnum=yes
- for name in "$@" ; do
- if [ "$name" = "-v" ] || [ "$name" = "--verbose" ] ; then
- continue
- elif [ "$name" = "-h" ] || [ "$name" = "--help" ] ; then
- printnum=no
- break
- elif [ ! -e "$name" ] ; then
- echo "del: File "$name" doesn't exist."
- else
- cd "$calldir"
- cd "`dirname "$name"`"
- wdir="`pwd`"
- file="`basename "$name"`"
- findmp
- settrash
- cd "$wdir"
- delete
- cd $calldir
- n=`expr $n + 1`
- fi
- done
- if [ "$printnum" = "yes" ] ; then
- if [ $n -eq 1 ] ; then
- echo "del: $n file sent to trash"
- else
- echo "del: $n files sent to trash"
- fi
- fi
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement