-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrecmd5.sh
executable file
·64 lines (56 loc) · 1.71 KB
/
recmd5.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#!/bin/bash
# create checksum file if neccessary
if [ ! -f ./checksums.md5 ]
then
touch checksums.md5
fi
# get files in directory (recursive) and list in checksum file
DIR_FILES=$(find . -type f ! -name 'checksums.md5' | sort -V | tr -d '\0')
MD5_FILES=$(cat checksums.md5 | cut -c 35- | sort -V | tr -d '\0')
# get list of new and deleted files
NEW_FILES=$(diff --new-line-format="" --unchanged-line-format="" \
<(echo "${DIR_FILES}") <(echo "${MD5_FILES}"))
DEL_FILES=$(diff --new-line-format="" --unchanged-line-format="" \
<(echo "${MD5_FILES}") <(echo "${DIR_FILES}"))
# deal with new files
if [ ! -z "${NEW_FILES}" ]
then
# list new files and add checksums
echo 'new files:'
echo '----------'
echo "${NEW_FILES}" | while IFS= read -r FILE
do
echo "${FILE}"
md5sum "${FILE}" >> checksums.md5
done
echo
# sort the checksum file again
sort -o checksums.md5 -k 2 -V checksums.md5
fi
# deal with deleted files
if [ ! -z "${DEL_FILES}" ]
then
# list deleted files
echo 'deleted files:'
echo '--------------'
echo "${DEL_FILES}"
echo
# ask if checksums of deleted files should be removed
read -p "Remove the checksums for these files? [y/N] " -n 1 -r REPLY
echo
if [[ ! ${REPLY} =~ ^[Yy]$ ]]
then
exit
fi
# remove checksums of deleted files
echo "${DEL_FILES}" | while IFS= read -r FILE
do
LINE_NUM=$(cat checksums.md5 | grep -Fn " ${FILE}" | cut -f1 -d':')
if [ $(echo "${LINE_NUM}" | wc -l) -ne 1 ]
then
echo "Multiple checksums for ${FILE} ... skipping" 1>&2
continue
fi
sed -i "${LINE_NUM}d" checksums.md5
done
fi