-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathfindfiles.sh
More file actions
executable file
·74 lines (62 loc) · 2.6 KB
/
findfiles.sh
File metadata and controls
executable file
·74 lines (62 loc) · 2.6 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
#!/bin/bash
# this is a hyper-specific one-off script that's probably not worth productizing made in March of 2026
# args: findfiles.sh [source dir] [target dir]
#
# what this script does is finds files non-recursively in source-dir then finds dupe files (recursively) in target dir
#
# 'finding a file' is simple filename match
#
# first an attempt will be made to find any dupes with exact file name,
# then if none are found, an attempt is made with the filename with extension removed
#
# example:
#
# source file: /Users/jason/mysourcedir/10241215.jpg
# target dupe file #1: /Users/jason/targetdir/someotherdir/10241215.jpg # identical file match
# target dupe file #2: /Users/jason/targetdir/someotherdir2/10241215.jpg # identical file match, diff directory from previous
# target dupe file #3: /Users/jason/targetdir/someotherdir2/10241215.jpeg # note mismatched file extension
#
# in this example, if the dupe files #1 and #2 exist, they will printed out as dupes and dupe file #3 will not
# if, however, dupe files #1 AND #2 do not exist, current impl of the script will print dupe file #3 only
#
# copyright jason baker (jason@onejasonforsale) 2026
#make for's argument seperator newline only
IFS=$'\n'
SCRIPT_DIR=`dirname "${0}"`
SOURCE_DIR="${1}"
TARGET_DIR="${2}"
TARGET_FILES_LIST="${SCRIPT_DIR}/targetfileslist.txt"
echo "SCRIPT_DIR: ${SCRIPT_DIR}"
echo "SOURCE_DIR: ${SOURCE_DIR}"
echo "TARGET_DIR: ${TARGET_DIR}"
echo "TARGET_FILES_LIST: ${TARGET_FILES_LIST}"
find "${TARGET_DIR}" -type f | sort > "${TARGET_FILES_LIST}"
FILE_COUNT=0
FILES_WITH_DUPES_COUNT=0
for FILE in `find ${SOURCE_DIR} -type f | sort`; do
((FILE_COUNT=FILE_COUNT + 1))
FILENAME=`basename "${FILE}"`
FILENAME_WITHOUT_EXTENSION="${FILENAME%%.*}" # example, blah.tar.sh -> blah
echo "Current source file: ${FILE} (name: ${FILENAME}) (without extension: ${FILENAME_WITHOUT_EXTENSION})"
DUPE_COUNT=0
for DUPE in `grep "${FILENAME}" "${TARGET_FILES_LIST}"`; do
echo "Found dupe: ${DUPE}"
((DUPE_COUNT=DUPE_COUNT + 1))
done
if [ ${DUPE_COUNT} = "0" ]; then
echo "Couldn't find dupes so far, trying without extension"
for DUPE in `grep "${FILENAME_WITHOUT_EXTENSION}" "${TARGET_FILES_LIST}"`; do
echo "Found dupe: ${DUPE}"
((DUPE_COUNT=DUPE_COUNT + 1))
done
fi
if [ ${DUPE_COUNT} = "0" ]; then
echo "Could not find dupes for ${FILE}"
else
((FILES_WITH_DUPES_COUNT=FILES_WITH_DUPES_COUNT + 1))
echo "Found ${DUPE_COUNT} dupes for ${FILE}"
fi
echo ""
done
NON_DUPE_FILE_COUNT=$((FILE_COUNT - FILES_WITH_DUPES_COUNT))
echo "Processed ${FILE_COUNT} files. ${FILES_WITH_DUPES_COUNT} had dupes, ${NON_DUPE_FILE_COUNT} did not have dupes."