Este é um script muito difícil de tentar fazer o que você quer. Sinta-se à vontade para copiar, modificar, otimizar - seria bom ter alguma resposta se funcionar para você. Se testou na minha pasta "Downloads" e encontrou apenas um erro restante (um nome de arquivo contendo [ , que grep não gostou).
Editar: Modificada a fonte novamente, pois a criação de registros de data e hora (na primeira versão) não é mais necessária, pois arquivos novos / modificados são encontrados com find -newer . Também foram adicionados parâmetros para definir o nome do arquivo de hash e, possivelmente, a pasta de início para começar; então o script não precisa ser chamado no diretório principal.
#!/bin/bash
#
# Script to create md5 hashes for files in and below the current directory
# or the directory passed at the commandline
# In the first run, create the sums for all files.
# In the second run,
# - if the files have not changed, keep the entries
# - if the files have been deleted, forget the entry
# - if the files have changed, create new md5 hash.
#
# Rough version - should be optimized
#
if [ $# -lt 1 ] ; then
echo "Usage:"
echo "$0 <hashfile> [<topdir>]"
echo
exit
fi
export HASHFILE=$1
export TOPDIR='.'
if [ $# -eq 2 ] ; then TOPDIR=$2; fi
export BACKFILE=$HASHFILE.bck
export TMPFILE=$HASHFILE.tmp
# In the first run, we create the file $HASHFILE if it does not exist
# You have to make sure that $HASHFILE does not contain any garbage for the first run!!
if [ ! \( -f $HASHFILE -a -s $HASHFILE \) ]; then
echo -n "Creating $HASHFILE for the first time..."
find $TOPDIR -type f -print0 | xargs -0 md5sum > $HASHFILE
echo "done."
exit
fi
# In the second run, we proceed to find the differences.
# First, find the newer files
find $TOPDIR -type f -newer $HASHFILE -print > $TMPFILE
# Now save the old file and create a new one, starting with new files
mv $HASHFILE $BACKFILE
echo -n "Processing new or modified files ..."
cat $TMPFILE | while read filename ; do
md5sum "$filename" >> $HASHFILE
done
echo "done."
# Now walk through the old file and process to new file
cat $BACKFILE | while read md5 filename ; do
# Does the file still exist?
if [ -f "$filename" ] ; then
# Has the file been modified?
if grep -q -e "^$filename$" $TMPFILE ; then
echo "$filename has changed!"
else
echo "$md5 $filename" >> $HASHFILE
#echo "$filename has not changed."
fi
else
echo "$filename has been removed!"
fi
done
# We now may delete temporary files
# rm $BACKFILE
# rm $TMPFILE
exit