Xfce Forum

Sub domains
 

You are not logged in.

#1 2016-04-03 12:22:19

Misko_2083
Member
Registered: 2015-10-13
Posts: 191
Website

md5 checker with progress bar

Sharing the bash script I made last night.
This script calculates md5 for single and multiple files selected. For single files it has the option to compare the calculated checksum with the one usually provided in md5 files.
Dependancies are pv, zenity and xsel (optional or in other words just for fun big_smile ). If xsel isn't installed the script will open a text box so you can manualy enter/paste md5 and also display a small tip to install xsel. xsel is used to auto-use the checksum in the clipboard.

Setup:
Save the script to /usr/local/bin/md5checksum.sh, make executable chmod +x
Set the thunar custom action
Command:

temp=$(mktemp /tmp/XXXXXX); > $temp; for file in %N; do echo $file >> $temp; done; /usr/local/bin/md5checksum.sh $temp; rm -f $temp

Appearance conditions:
Pattern: *
Appers for: Audio, Video, Text, Image and Other Files

#!/bin/bash
# /usr/local/bin/md5checksum.sh
# Depends on zenity, pv, xsel(optional)
# Thunar Custom Action
# Command: temp=$(mktemp /tmp/XXXXXX); > $temp; for file in %N; do echo $file >> $temp; done; /usr/local/bin/md5checksum.sh $temp; rm -f $temp
# Appearance conditions:
#   Pattern: *
#   Appers for: Audio, Video, Text, Image and Other Files

file="$@"

count="$(wc -l "$file" | cut -d' ' -f1)"

hash zenity 2>/dev/null
if [[ $? != 0 ]]; then
   echo "zenity is not installed!"
   echo "please install zenity"
   exit 1
fi

if [[ "$count" = "0" ]]; then
   zenity --error --text="Nothing to calculate!"

elif [[ "$count" = "1" ]]; then
    hash pv 2>/dev/null
    if [[ $? != 0 ]]; then
       echo "pv is not installed!"
       echo "please install pv"
       zenity --error --text="pv is not installed\nplease install pv"
       exit 1
    fi

    sum_temp=$(mktemp /tmp/XXXXXXX)
   ( pv -n "$(cat ${file})" | md5sum 2>&1 | tee >(cut -d ' ' -f1 >  $sum_temp) 2>&1 ) 2>&1 \
   | awk '{if ($1 <=99) print $1; else print 99; fflush(stdout)}' \
   | zenity --progress --title="MD5sum" --text="Calculating MD5 for:\n$(cat ${file})" --percentage=0 --auto-close

   if [[ $? != 0 ]]; then
        rm -f $sum_temp
   else
       if zenity --question --title="MD5sum" \
                 --text="MD5sum : $(cat $sum_temp)\nFile :          $(cat ${file})\n\nIf you have the correct checksum in the clipboard you can compare it with this one.\nCopy the MD5sum from the *.md5 file and then click Yes to compare.\nDo you want to compare?" \
                 --no-wrap 
       then
           hash xsel 2>/dev/null
           if [[ $? = 0  && "$(xsel -b | wc -c)" = "32" ]]; then
               sum_paste="$(xsel -b)"
               if [[ "$sum_paste" == "$(cat $sum_temp)" ]];then
                   zenity --info --title="MD5sum" \
                          --text="Checksums are IDENTICAL!" --icon-name="gtk-yes"
               else
                   zenity --info --title="MD5sum" \
                          --text="Checksums are DIFFERENT!\n\nIn Clipboard:\n$sum_paste\n$(cat $file):\n$(cat $sum_temp)" --icon-name="gtk-no"
               fi
          
           else
              sum_paste="$(zenity --entry --title="MD5sum" \
                                  --text="$(hash xsel 2>/dev/null; if [[ $? != 0 ]]; then echo 'Tip: Install xsel to auto-use the md5 copied to the clipboard.\n\n';fi)Enter the sum to compare:" \
                                  --entry-text="Paste Here")"
              if [[ $? = 0 ]]; then
                 if [[ "$sum_paste" == "$(cat $sum_temp)" ]];then
                     zenity --info --title="MD5sum" \
                            --text="Checksums are IDENTICAL!" --icon-name="gtk-yes"
                 else
                     zenity --info --title="MD5sum" \
                            --text="Checksums are DIFFERENT!" --icon-name="gtk-no"
                 fi
              fi
           fi
       fi
       rm -f $sum_temp
   fi

elif [[ $count -gt 1 ]]; then
   if zenity --question --title="MD5sum" \
             --text="Multiple files selected.\nThis will create/overwrite a file named <b>hash.md5</b>\nin the directory where the selected files are stored.\nProceed?" \
             --no-wrap
       then
           > hash.md5
           i=0
           TOTAL="$count"

           while read -r line || [[ -n "$line" ]]; do
                ((++i))
                PERCENT=$(($i*100/${TOTAL}))
                echo "#md5sum $i/$TOTAL: $line"
                md5sum "${line}" >> hash.md5 2>&1
                echo "$PERCENT"
           done < "$file" | zenity --progress --title="MD5" --auto-close

           exo-open 'hash.md5' || zenity --text-info --title="MD5 checksums" < hash.md5
   fi
fi

Do you want to exit the Circus?
https://www.youtube.com/watch?v=ZJwQicZHp_c

Offline

#2 2016-04-03 12:51:23

ToZ
Administrator
From: Canada
Registered: 2011-06-02
Posts: 11,023

Re: md5 checker with progress bar

Very nice. Thank you for sharing.


Please remember to mark your thread [SOLVED] to make it easier for others to find
--- How To Ask For Help | FAQ | Developer Wiki  |  Community | Contribute ---

Offline

#3 2016-04-04 12:55:38

Misko_2083
Member
Registered: 2015-10-13
Posts: 191
Website

Re: md5 checker with progress bar

Thank you ToZ smile
I have a few more in case you are interested smile

Last edited by Misko_2083 (2016-04-05 11:05:14)


Do you want to exit the Circus?
https://www.youtube.com/watch?v=ZJwQicZHp_c

Offline

Board footer

Powered by FluxBB