summaryrefslogtreecommitdiff
path: root/bin/duplicate.sh
blob: c35f46f6d3fbe126865d362fd39fab9fbe6d7df9 (plain)
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
#!/bin/bash

# Example usage, find all doubles in all sub directory
# 	for i in *; do if [ -d $i ]; then double.sh $i; fi; done
#
# To find duplicates in specific directory
# 	double.sh /path/to/dir /path/to/other/dir ./relativ/path
#
# With zero paramater, the script uses the working diroctory.

DATABASE="/tmp/double.db"
DIR="./found_doubles"
COUNT=0
SDIR=$(pwd)

if [ $1 = "-a" ]; then
	ALL=0
	shift
else
	ALL=1
fi

if [ $# -eq 0 ]; then
	LOOP=false
else
	LOOP=true
fi

echo ""

while true; do 

	if [ $LOOP = true ]; then
		cd $SDIR
		while true; do
			if [ $# -eq 0 ]; then
				exit 0
			fi

			if [ ! -d "$1" ]; then
				echo "-- Can't chdir to $1. Shifting."
				shift
			else
				echo ""
				echo "Changing directory to '$1'."
				cd "$1"
				PWD=$(pwd)
				shift
				break
			fi
		done
	else
		echo "Working in directory '$(pwd)'."
	fi

	sqlite3 $DATABASE "CREATE TABLE files (id INTEGER PRIMARY KEY, file TEXT, hash TEXT UNIQUE)"
	mkdir -p $DIR
	TMP=0
	for i in *; do
        	if [ -f "$i" ]; then
                	HASH=$(md5sum "$i" | awk '{ print $1 }')
	                sqlite3 $DATABASE "INSERT INTO files VALUES (NULL, '$i', '$HASH')" 2>/dev/null
			if [ ! $? -eq 0 ]; then
				echo "----------------------------"
				echo "Found a duplicate: "
				echo "$i"
				sqlite3 $DATABASE "SELECT file FROM files WHERE hash='$HASH'"
				echo "Moving $i to $PWD $DIR"
				mv "$i" "$DIR"
				((COUNT ++))
				((TMP ++))
			fi
        	fi
	done

	if [ $TMP -eq 0 ]; then
		rmdir $DIR
	fi

	if [ $LOOP = true ]; then
		if [ $ALL -eq 1 ]; then
			sqlite3 $DATABASE "DROP TABLE files"
		fi
		echo ""
	else
		break
	fi

	if [ $# -eq 0 ]; then
		break;
	fi
done

echo "Found and moved $COUNT files to $DIR."
rm -f $DATABASE

exit 0