-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathfind_dups
More file actions
executable file
·69 lines (60 loc) · 1.64 KB
/
find_dups
File metadata and controls
executable file
·69 lines (60 loc) · 1.64 KB
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
65
66
67
68
69
#!/usr/bin/env bash
# Find duplicate files using checksum..
# -- VARIABLES -- #
SCRIPT=`basename $0` # Script name without path
SCRIPT_PATH=$(dirname $SCRIPT) # Script path
SCRIPT_NM=`echo $SCRIPT | awk -F"." '{print $1}'` # Script name without any extension
DT_STAMP=`date "+%Y%m%d_%H%M%S"`
TMP_FILE=/tmp/${SCRIPT_NM}-${DT_STAMP}.tmp
LOG_FILE=/tmp/${SCRIPT_NM}-${DT_STAMP}.log
# Exit values
EXIT_ERR=1
EXIT_SUCC=0
# -- FUNCTIONS -- #
show_usage()
{ # Show script usage
echo "
${SCRIPT} - Shell script to find duplicate files using checksum check.
The result is placed in ${LOG_FILE}.
USAGE
${SCRIPT} [OPTIONS]
OPTIONS
-r Flag to remove the duplicates found, leaving only one original.
-h
Display this help screen.
"
}
# -- MAIN -- #
while getopts ":rh" optval "$@"; do
case $optval in
"r") # Remove duplicates, leaving one original (first)
RM_DUPS="Y"
;;
"h") # Print help and exit
show_usage
exit ${EXIT_SUCC}
;;
"?") # Print help and exit
echo "Invalid option -${OPTARG}"
show_usage
exit ${EXIT_ERR}
;;
:)
echo "Option -${OPTARG} requires an argument"
show_usage
exit ${EXIT_ERR}
;;
*)
echo "Invalid option with parameter -${OPTARG}"
show_usage
exit ${EXIT_ERR}
;;
esac
done
# Find duplicates using checksum check
find . -size 20 \! -type d -exec cksum {} \; | sort | tee ${TMP_FILE} | cut -f 1,2 -d ‘ ‘ | uniq -d | grep -hif – ${TMP_FILE} > ${LOG_FILE}
# Remove duplicates
if [ "$RM_DUPS" = "Y" ]; then
while read file; do rm “$file”; done < ${LOG_FILE}
fi
# -- END -- #