Advertisement
mosaid

duplicates.sh

Aug 26th, 2019
388
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 1.26 KB | None | 0 0
  1. #!/bin/bash
  2. #this script will search for duplicate files
  3. #if it finds duplicates it will write the path of the file
  4. #and its duplicates for later manual operations
  5. #
  6. cd "your/files/directory"
  7. outputDir="${HOME}/testDups"
  8. files=$( find "$PWD" -type f 2>/dev/null )
  9. i=1
  10. while true ; do
  11.     NB=$(echo "$files" | wc -l )
  12.     percentage=$( bc -l <<< "scale=2; $i * 100 / $NB" )
  13.     printf '%7d/%d  : %3.2f%%\n' "$i" "$NB"   "$percentage"
  14.     echo -en "\e[1A"
  15.     file=$(echo "$files" | sed -n '1p' )     #read the first line (file)
  16.     files=$(echo "$files" | sed "1d" )       #remove the line we just read
  17.     size=$(stat -c%s "$file")
  18.     while read -r l ; do
  19.         s=$(stat -c%s "$l")
  20.         if (( $s == $size )) ; then
  21.             [[ ! -f "$outputDir/$i" ]] && echo "$file" > "$outputDir/$i"    #save the file
  22.             echo "$l" >> "$outputDir/$i"                                    #and its duplicate
  23.         fi
  24.     done <<< "$files"
  25.     if [[ -f "$outputDir/$i" ]] ; then
  26.         while read -r ll ; do
  27.             files=$(echo "$files" | sed "\|$ll|d" )      #remove the file and its duplicates from the list
  28.         done < "$outputDir/$i"                           #to speed up the process
  29.     fi
  30.     (( $NB <=1 )) && break
  31.     i=$((i+1))
  32. done
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement