Compare files from a directory with md5sum and shellscript

2

Good morning!

I'm studying shellscript and an exercise asks for a scan of files in the current directory and md5 hashes to be computed. It also asks that if there are identical files by comparing hashes, these files are printed. The code I was able to do gets the result, but it gets duplicated; I can not remove a file from the next scans once it has already been plotted as equal to another. Detail: You can not use redirection for temporary files.

#!/bin/bash

ifs=$IFS
IFS=$'\n'

echo "Verificando os hashes dos arquivos do diretório atual..."

for file1 in $(find . -maxdepth 1 -type f | cut -d "/" -f2); do
  md51=$(md5sum $file1  | cut -d " " -f1)
  for file2 in $(find . -maxdepth 1 -type f | cut -d "/" -f2 | grep -v "$file1"); do
    md52=$(md5sum $file2 | cut -d " " -f1)
    if [ "$md51" == "$md52" ]; then
      echo "Arquivos $file1 e $file2 são iguais."
    fi
  done
done

I would also like to know if there is a more efficient way of doing this.

Thanks in advance for the help!

    
asked by anonymous 29.03.2017 / 15:43

1 answer

1

Dude, I was able to do with awk , see if it helps you:

md5sum * | awk '{if(length(te[$1]) == 1) print "O arquivo: " te[$1] " e " $2  " são iguais."; else te[$1] = $2 }'

Explaining:

  • md5sum * will calculate the hash of all files in the current directory.

  • if(length(te[$1]) == 1) checks if the hash value has already been assigned to the array.

  • print "O arquivo: " te[$1] " e " $2 " são iguais." if it has already been assigned before it displays the message on the screen.

  • else te[$1] = $2 if it has not been assigned to the array, it will assign the file name with the hash key.

You can see some examples of awk here .

    
31.03.2017 / 20:28