clean_up_trunk_images.sh

Joel Mckay, 2012-05-04 07:28 pm

Download (2.9 kB)

 
1
#!/bin/bash
2
outDir=$(pwd)
3
4
echo "This will convert or delete anything that is not a unique jpg file."
5
echo "It will scan the current path, and every subdirectory in this path."
6
read -p "Press [Enter] key to start..."
7
8
echo "======================================================"
9
echo "path: $outDir"
10
echo "======================================================"
11
read -p "Press [Enter] if you are sure you want to scrub the current path..."
12
13
14
echo "$outDir"  -type f -name \'*.jpg\' -or -name \'*.JPG\' -name \'*.jpeg\' -or -name \'*.JPEG\' -or -name \'*.png\' -or -name \'*.PNG\' -or -name \'*.gif\' -or -name \'*.GIF\' -or -name \'*.bmp\' -or -name \'*.BMP\'  | xargs find | xargs chmod 600
15
16
echo "- - - - - - - - - - - Scanning Context Trunk - - - - - - - - - - - "
17
	
18
##################################
19
#Convert PNG to JPG files
20
echo "$outDir" -type f  -name \'*.png\' -or -name \'*.PNG\' -or -name \'*.gif\' -or -name \'*.GIF\' -or -name \'*.bmp\' -or -name \'*.BMP\' | xargs find  > "$outDir/pnglist.txt" 
21
counter=0
22
while read line
23
do
24
	#item counter
25
	counter=$(($counter+1))
26
    
27
	xpath=${line%/*} 
28
	xbase=${line##*/}
29
	xfext=${xbase##*.}
30
	xpref=${xbase%.*}
31
	
32
	newFleName="$xpath/"$counter"_converted.jpg"
33
	
34
	if [ -f "$line" ] && [ ! -f "$newFleName" ]
35
	then
36
		convert "$line" "$newFleName" 
37
		rm -f "$line"
38
		echo -ne "."
39
	fi
40
    
41
done < "$outDir/pnglist.txt"
42
rm "$outDir/pnglist.txt"
43
44
##################################
45
#Index JPG files
46
echo "$outDir" -type f -name \'*.jpg\' -or -name \'*.jpeg\' -or -name \'*.JPG\' -or -name \'*.JPEG\' | xargs find  > "$outDir/jpglist.txt"
47
rm -f "$outDir/taglist.txt"
48
rm -f "$outDir/hashlist.txt"
49
touch "$outDir/taglist.txt"
50
touch "$outDir/hashlist.txt"
51
counter=0
52
while read line
53
do
54
	#item counter
55
	counter=$(($counter+1))
56
    
57
	xpath=${line%/*} 
58
	xbase=${line##*/}
59
	xfext=${xbase##*.}
60
	xpref=${xbase%.*}
61
	#clean out name
62
	#cleanName=$( echo "$xpref" | sed 's/[^A-Za-z0-9\_\.]/_/g' )
63
	
64
	newFleName="$xpath/$counter.jpg"
65
	
66
	md5hash=`md5sum "$line" | awk '{ print $1 }'`
67
	
68
	if grep -q "$md5hash" "$outDir/hashlist.txt"
69
	then
70
			echo "Delete Dupe: " $line
71
			rm -f  "$line"
72
	else
73
		echo "$newFleName" | cat >> "$outDir/taglist.txt"
74
		echo -ne "Checking $counter            \r"		
75
		
76
		if [ -f "$line" ] && [ ! -f "$newFleName" ]
77
		then
78
				mv -f "$line" "$newFleName" 
79
				echo "$md5hash" | cat >> "$outDir/hashlist.txt"
80
				#slow, but needed to prevent missing the grep check above...
81
				sync	
82
		fi
83
	fi
84
    
85
done < "$outDir/jpglist.txt"
86
rm "$outDir/jpglist.txt"
87
 
88
 
89
###############################################
90
#clean up unknowns
91
echo "$outDir" -type f -not -name \'*.jpg\' -and -not -name \'unknownlist.tar.gz\' -and -not -name \'taglist.txt\'   -and -not -name \'contextlist.txt\' -and -not -name  \'hashlist.txt\'  -and -not -name \'unknownlist.txt\' -and -not -name \'clean_up_trunk_images.sh\' | xargs find  > "$outDir/unknownlist.txt"
92
if [ -s "$outDir/unknownlist.txt" ]
93
then	
94
	tar -cazf unknownlist.tar.gz  -T "$outDir/unknownlist.txt" --remove-files
95
fi
96
97
exit 0