-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcopynew.sh
executable file
·98 lines (73 loc) · 2.37 KB
/
copynew.sh
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
#!/bin/bash
srcDirectory="/Users/tongpingliu/Pictures/iPhoto Library_2.photolibrary/Masters/2019/"
#20180615-183907"
#/Users/tongpingliu/Pictures/iPhoto Library_2.photolibrary/Masters";
destDirectory="/Volumes/FreeAgent GoFlex Drive/picture/backup";
subDirectories=("05");
#subDirectories=("2014" "2015" "2016");
printf "$srcDirectory\n";
# Delete the file that will be analyzed
cd "$srcDirectory";
rm -f ToBeAnalyzed;
mkdir -p "$destDirectory";
#echo "test" > ToBeAnalyzed;
# Check each subdirectory one by one.
for dir in "${subDirectories[@]}"
{
# Finding out the file in this directory.
# $srcDir = $srcDirectory"./"$dir
# $srcDir = "$srcDirectory/$dir";
printf "source directory is $dir\n";
find $dir -iname "*.jpg" >> ToBeAnalyzed;
find $dir -iname "*.MOV" >> ToBeAnalyzed;
}
# Now the files are found out.
# array=(`echo $dir | tr "/" " "`);
# for i in "${!array[@]}"
# do
# echo "$i=>${array[i]}"
# done
while read file; do
# Fetch the first two directory to combine to a new directory.
echo $file;
filearray=(`echo $file | tr "/" " "`);
filename=`basename $file`;
newdir=$(stat -f "%Sm" -t "%Y%m" $file);
#newdir="${filearray[0]}${filearray[1]}";
destdir=$destDirectory"/"$newdir;
echo "destdir is $destdir";
destfile=$destdir"/"$filename;
mkdir -p "$destdir";
# find out all files with the same name and output to a file.
filesize=`stat -f "%z" "$file"`;
find "$destdir" -name $filename > samenamefiles;
# Check every file in the list
toCopyFile="true";
hasSameNameFile="false";
while read samenamefile; do
echo "samefile "$samenamefile"";
newsize=`stat -f "%z" "$samenamefile"`;
# check whether this file is existing or not
# It is impossible for two different files that have the same name and have the same size
if [ $newsize -lt $filesize ]; then
toCopyFile="true";
break;
elif [ "$destfile" == "$samenamefile" ]; then
hasSameNameFile="true";
toCopyFile="false";
fi
done < samenamefiles;
# if we are trying to copy the file
if [ $toCopyFile == "true" ]
then
echo "copying $destfile";
echo "$destfile" >> copfile;
cp -f "$file" "$destfile"
else
echo "no need to copy $destfile"
fi
# First, let's check whether this file exists or not: the same name and size
# if[ -f
# Otherwise, copy to the new directory.
done < ToBeAnalyzed;
#mytest;