/
GTU6-2.sh
executable file
·75 lines (66 loc) · 2.37 KB
/
GTU6-2.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
#!/usr/bin/env bash
# Code written By: Rushyang Darji
# Last Build: 09.10.2010
# My Online Repository: http://github.com/rushyang/GTU-OS-Bash-Scripts
while true; do
read -e -p "Enter first Directory's Absolute path: " path1 || exit
[[ -d $path1 ]] && break
echo "Invalid path, Try Again!"
done
while true; do
read -e -p "Enter second Directory's Absolute path: " path2 || exit
[[ -d $path2 ]] && break
echo "Invalid path, Try Again!"
done
while true; do
read -e -p "Enter Third Directory's Path, to copy files in case of exact match: " path3 || exit
[[ -d $path3 ]] && break
echo "Invalid Path, Try, Again!"
done
temp=$(mktemp)
for i in $path1/*
do
if [ -f "$i" ]; then
for j in $path2/*
do
if [ -f "$j" ]; then
base1=`basename "$i"`
base2=`basename "$j"`
if [ "$base1" = "$base2" ]; then
diff "$i" "$j" > $temp
size=`ls -s $temp | awk '{print $1}'`
if [ "$size" -eq "0" ]; then
echo "File: \"$base1\" was found same in both directories."
cp "$i" "$path3"
echo "Copied to \"$path3\" successfully!"
fi
fi
fi
done
fi
done
rm $temp
: << --
OUTPUT (Attempt 1)
rushyang@Maverick_Meerkat: GTU-MCA $ bash GTU6-2.sh
Enter first Directory's Absolute path: /home/rushyang/sldjf
Invalid path, Try Again!
Enter first Directory's Absolute path: /home/rushyang/Experiments/1510/mydir1/
Enter second Directory's Absolute path: /home/rushyang/lsajlj
Invalid path, Try Again!
Enter second Directory's Absolute path: /home/rushyang/Experiments/1510/mydir2/
Enter Third Directory's Path, to copy files in case of exact match: /home/rushyang/Experiments/1510/mydir3/
File: "1" was found same in both directories.
Copied to "/home/rushyang/Experiments/1510/mydir3/" successfully!
File: "new file" was found same in both directories.
Copied to "/home/rushyang/Experiments/1510/mydir3/" successfully!
OUTPUT (Attempt 2)
rushyang@Maverick_Meerkat: GTU-MCA $ bash GTU6-2.sh
Enter first Directory's Absolute path: /home/rushyang/Experiments/1510/mydir1/
Enter second Directory's Absolute path: /home/rushyang/Experiments/1510/mydir2/
Enter Third Directory's Path, to copy files in case of exact match: /home/rushyang/Experiments/1510/mydir3/
File: "1" was found same in both directories.
Copied to "/home/rushyang/Experiments/1510/mydir3/" successfully!
File: "new file" was found same in both directories.
Copied to "/home/rushyang/Experiments/1510/mydir3/" successfully!
--