Last active
November 8, 2023 12:58
-
-
Save rdenadai/a3df04fa46ccbe4fdaab5794e11659af to your computer and use it in GitHub Desktop.
Bash script which generate checksum from a list of dirs and files located inside a file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
checksum() { | |
FILE=$1 | |
if [ "$ALGO" = "SHA1" ]; then | |
sha1sum $FILE | |
else | |
md5sum $FILE | |
fi | |
} | |
generate() { | |
PASSED=$1 | |
if [ -f "${PASSED}" ]; then | |
checksum $PASSED | |
fi | |
} | |
allFiles() { | |
FARRAY=() | |
WHICH=$1 | |
PATH_=$2 | |
if [ -d "${PATH_}" ]; then | |
FILES="$(find $PATH_)" | |
for F1 in $FILES ; do | |
if [ -f "${F1}" ]; then | |
# echo $F1 | |
FARRAY+=($F1) | |
fi | |
done | |
else | |
if [ -f "${PATH_}" ]; then | |
FARRAY+=($PATH_) | |
fi | |
fi | |
if [ "$WHICH" -eq 1 ]; then | |
ARRAY1+=("${FARRAY[@]}") | |
else | |
ARRAY2+=("${FARRAY[@]}") | |
fi | |
} | |
echo '-------------------------------' | |
LOOK=$1 | |
ALGO=$2 | |
DIR1=$3 | |
DIR2=$4 | |
echo 'COLLECTION: '$LOOK | |
echo 'ALGORITHM: '$ALGO | |
echo 'DIR_1: '$DIR1 | |
echo 'DIR_2: '$DIR2 | |
ARRAY1=() | |
ARRAY2=() | |
for F in $(cat $LOOK | sort) ; do | |
allFiles 1 $DIR1$F | |
allFiles 2 $DIR2$F | |
done | |
echo '' | |
echo '======================FILES FROM: '$DIR1' ======================' | |
for GEN1 in sort ${ARRAY1[*]} ; do | |
generate $GEN1 | |
done | |
echo '' | |
echo ' ======================FILES FROM: '$DIR2' ======================' | |
for GEN2 in sort ${ARRAY2[*]} ; do | |
generate $GEN2 | |
done | |
mkdir dir1 2>/dev/null | |
cp ${ARRAY1[*]} dir1/ 2>/dev/null | |
mkdir dir2 2>/dev/null | |
cp ${ARRAY2[*]} dir2/ 2>/dev/null | |
echo '' | |
echo '======================TAR OF FILES: '$DIR1' ======================' | |
cd dir1 | |
if [ "$ALGO" = "SHA1" ]; then | |
find | xargs tar cvf - | sha1sum | |
else | |
find | xargs tar cvf - | md5sum | |
fi | |
echo '' | |
echo '======================TAR OF FILES: '$DIR2' ======================' | |
cd ../dir2 | |
if [ "$ALGO" = "SHA1" ]; then | |
find | xargs tar cvf - | sha1sum | |
else | |
find | xargs tar cvf - | md5sum | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Gerador de checksum
Pequeno script que gera o checksum (SHA1 ou MD5) de uma lista de arquivos existente em dois diretórios diferentes. Permitindo assim a verificação dos hash's gerados.
Como utilizar
Crie uma listagem contendo todos os nomes dos arquivos, ou os diretórios os quais pretende que sejam avaliados.
Execute o script conforme abaixo, passando os parâmetros necessários
Exemplo
$> ./compare.sh lista.txt SHA1 /home/user1 /home/user2