-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathdupe.sh
More file actions
executable file
·81 lines (68 loc) · 2.92 KB
/
dupe.sh
File metadata and controls
executable file
·81 lines (68 loc) · 2.92 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
70
71
72
73
74
75
76
77
78
79
80
81
#!/usr/bin/env bash
# Load environment variables from .env file if it exists
# in the same directory as this bash script
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
ENV_PATH="$SCRIPT_DIR/.env"
if [ -f "$ENV_PATH" ]; then
# shellcheck source=.env
echo "Loading environment variables from $ENV_PATH file"
# shellcheck disable=SC1090 # shellcheck sucks
if source "$ENV_PATH"; then
echo "Environment variables loaded successfully"
else
echo "Error loading environment variables" >&2
exit 1
fi
else
echo ".env file not found in script directory ($ENV_PATH)"
fi
# Default Variables
JDUPES_OUTPUT_LOG=${JDUPES_OUTPUT_LOG:-"/mnt/data/jdupes.log"}
JDUPES_SOURCE_DIR=${JDUPES_SOURCE_DIR:-"/mnt/data/media/"}
JDUPES_DESTINATION_DIR=${JDUPES_DESTINATION_DIR:-"/mnt/data/torrents/"}
JDUPES_HASH_DB=${JDUPES_HASH_DB:-"/.config/jdupes_hashdb"}
JDUPES_COMMAND=${JDUPES_COMMAND:-"/usr/local/bin/jdupes"}
JDUPES_EXCLUDE_DIRS=${JDUPES_EXCLUDE_DIRS:-"-X nostr:.RecycleBin -X nostr:.trash"}
JDUPES_INCLUDE_EXT=${JDUPES_INCLUDE_EXT:-"mp4,mkv,avi"}
DEBUG=${DEBUG:-"false"}
find_duplicates() {
local log_file="$JDUPES_OUTPUT_LOG"
local start_time
start_time=$(date +%s)
echo "[$(date +"%Y-%m-%d %H:%M:%S")] Duplicate search started" | tee -a "$log_file"
if [ "$DEBUG" == "true" ]; then
echo "Running jdupes with:" | tee -a "$log_file"
echo "$JDUPES_COMMAND $JDUPES_EXCLUDE_DIRS -X onlyext:$JDUPES_INCLUDE_EXT -r -M -y $JDUPES_HASH_DB $JDUPES_SOURCE_DIR $JDUPES_DESTINATION_DIR" | tee -a "$log_file"
fi
local results
# shellcheck disable=SC2086
results=$("$JDUPES_COMMAND" $JDUPES_EXCLUDE_DIRS -X onlyext:"$JDUPES_INCLUDE_EXT" -r -M -y "$JDUPES_HASH_DB" "$JDUPES_SOURCE_DIR" "$JDUPES_DESTINATION_DIR")
if [[ $results != *"No duplicates found."* ]]; then
# shellcheck disable=SC2086
"$JDUPES_COMMAND" $JDUPES_EXCLUDE_DIRS -X onlyext:"$JDUPES_INCLUDE_EXT" -r -L -y "$JDUPES_HASH_DB" "$JDUPES_SOURCE_DIR" "$JDUPES_DESTINATION_DIR" >>"$log_file"
fi
if [ "$DEBUG" == "true" ]; then
echo -e "jdupes output: ${results}" | tee -a "$log_file"
fi
parse_jdupes_output "$results" "$log_file"
local finish_time
finish_time=$(date +%s)
local run_time=$((finish_time - start_time))
echo "[$(date +"%Y-%m-%d %H:%M:%S")] Duplicate search completed in ${run_time}s" | tee -a "$log_file"
}
parse_jdupes_output() {
local results="$1"
local log_file="$2"
if [[ $results != *"No duplicates found."* ]]; then
field_message="❌ Unlinked files discovered..."
parsed_log=$(echo "$results" | awk -F/ '{print $NF}' | sort -u)
else
field_message="✅ No unlinked files discovered..."
parsed_log="No hardlinks created"
fi
if [ "$DEBUG" == "true" ]; then
echo -e "$field_message" | tee -a "$log_file"
echo -e "Parsed log: ${parsed_log}" | tee -a "$log_file"
fi
}
find_duplicates