gc

Tuesday, January 8, 2013

Find and delete duplicate files from nautilus


( warning: backup the directory and files first, cause Im not responsible for any use of this script and losing data )
 
command from application that I use:
- fdupes
- notify-send
- zenity

operating system: Ubuntu linux 10.04

1. create /tmp/fD.sh file and add :
#!/bin/bash -x

OUTFILE=/tmp/fdups.out
LOGFILE=/tmp/fdups.log
DUPDIR=/tmp

function ntfy {
    `which notify-send` "fdups" "$MSG" --icon=dialog-information --expire-time=7000
}
function emsg {
    echo "$MSG"
    echo "    [i] $MSG" >> $LOGFILE;
}

[ ! -f $LOGFILE ] && touch $LOGFILE
echo "====================" >> $LOGFILE
echo `date` >> $LOGFILE
echo "cmd: $0 $*" >> $LOGFILE
echo "====================" >> $LOGFILE
echo . >> $LOGFILE

case "$1" in
            fdel)
                [ ! "$2" ] && { echo "use: $0 fdel <directory> "; exit; }
                DUPS=`$0 $2 | grep "Count of Duplicates" | cut -d":" -f2 | sort`;
                cat $OUTFILE;
                [ $DUPS -eq 0 ] && { MSG="no duplicate found in $2, exiting ..."; emsg; ntfy; exit; } || { MSG="total: $DUPS files found"; emsg; ntfy; }
                gedit $OUTFILE <&- 1>/dev/null 2>&1 &
                zenity --text "Delete $DUPS (leave 1 only for each) duplicate file in $2 (see output file in gedit editor)" --title "fdupes" --question;
                [ $? -eq 0 ] && { fdupes -rdN $2 >> $LOGFILE; } || { emsg; exit; }
            ;;
            fdelgui)
                [ ! "$2" ] && { echo "use: $0 fdel <directory> "; exit; }
                DUPS=`$0 gui $2 | grep "Count of Duplicates" | cut -d":" -f2 | sort`;
                DUPS=`cat ./DUPS`
                rm ./DUPS -fr;
                [ $DUPS -eq 0 ] && { MSG="no duplicate found in $2, exiting ..."; emsg; ntfy; exit; } || { MSG="total: $DUPS files found"; emsg; ntfy; }
                gedit $OUTFILE <&- 1>/dev/null 2>&1 &
                zenity --text "Delete $DUPS (leave 1 only for each) duplicate file in $2 (see output file in gedit editor)" --title "fdupes" --question;
                [ $? -eq 0 ] && { fdupes -rdN $2 >> $LOGFILE; } || { emsg; exit; }

            ;;
            gui)
                if [ "$1" ]; then
                (
                    echo "10" ; sleep 1
                    echo "# begin searching of duplicate file";
                    dupdir=$2
                    startdt=`date +%s`
                    echo "20" ;
                    echo "# running fdupes"; sleep 1
                    fdupes --recurse ${dupdir} > $OUTFILE
                    echo "70" ;
                    enddt=`date +%s`
                    ((diff_sec=enddt-startdt))
                    runtime=(`echo - | awk '{printf "  %d:%d:%d","'"$diff_sec"'"/(60*60),"'"$diff_sec"'"%(60*60)/60,"'"$diff_sec"'"%60}'`)
                    echo "# runtime: $enddt : $runtime"; sleep 1
                    echo "80" ; sleep 1
    echo "Total Runtime: ${runtime}"
                    grep ${dupdir} $OUTFILE | wc -l > ./DUPS
                    cntOfDupes=`cat ./DUPS`
                    ((cntOfFD=`find ${dupdir} | wc -l`-1))   #subtract 1 as this counts the current dir
                    echo "# Count of Duplicates: ${cntOfDupes}"; sleep 1
                    echo "# Count of Files/Directories: ${cntOfFD}"; sleep 1
                    echo "99" ;
                    echo "# finish"; sleep 1
                    echo "100";
                    ) |
                    zenity --auto-close --progress \
                    --title="$0 $* ..." \
                    --text="Memulai...                                                          *" \
                    --percentage=0

                    if [ "$?" = -1 ] ; then
                        zenity --error --text="Process canceled."
                    fi
                else
                    exit;
                fi
            ;;
            *)
                dupdir=$1
                du -hs ${dupdir} 2>/dev/null
                startdt=`date +%s`
                fdupes --recurse ${dupdir} > $OUTFILE
                enddt=`date +%s`
                ((diff_sec=enddt-startdt))
                runtime=(`echo - | awk '{printf "  %d:%d:%d","'"$diff_sec"'"/(60*60),"'"$diff_sec"'"%(60*60)/60,"'"$diff_sec"'"%60}'`)
                echo "Total Runtime: ${runtime}"
                cntOfDupes=`grep ${dupdir} $OUTFILE | wc -l`
                ((cntOfFD=`find ${dupdir} | wc -l`-1))   #subtract 1 as this counts the current dir
                echo "Count of Duplicates: ${cntOfDupes}"
                echo "Count of Files/Directories: ${cntOfFD}"
                cat $OUTFILE
                ;;
esac
2. go to $HOME/.gnome2/nautilus-scripts and create file, example: "find_duplicate_file" ,and add this :
#!/bin/sh
`which notify-send` "Search for duplicate files" "$NAUTILUS_SCRIPT_SELECTED_FILE_PATHS" --icon=dialog-information --expire-time=5000
exec /tmp/fD fdelgui '$NAUTILUS_SCRIPT_SELECTED_FILE_PATHS'

open nautilus, left click -> Scripts -> find_duplicate_file

don't forget to make this 2 file executable :
$ chmod u+x /tmp/fD.sh && chmod u+x $HOME/.gnome2/nautilus-scripts/find_duplicate_file

fin





No comments:

Post a Comment

Not Realy Free Premium Domain