Compare files from a directory with md5sum and shellscript

Asked

Viewed 522 times

2

Good morning!

I am studying shellscript and an exercise asks for a scan of files in the current directory and to calculate the md5 hashes. It also asks that, in case there are identical files by comparing the hashes, these files be printed. The code I was able to do achieves the result, but it is duplicated; I cannot remove a file from the next scans once it has already been plotted as equal to another. Detail: cannot use redirect to 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 task.

Thanks in advance for your help!

1 answer

1

Dude, I got to do with the awk, see if this 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 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.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.