Write a script to copy the file system from two directories to a new directory in such a way that only the latest file is copied in case there are common files in both the directories.
# read directory name
echo "Enter First Directory : \c"
read dir1
echo "Enter Second Dirctory : \c"
read dir2
# check inputed name is directory or not
if [ ! -d $dir1 ]
then
echo "DIRECTORY [ $dir1 ] NOT EXIST"
exit 1
fi
if [ ! -d $dir2 ]
then
echo "DIRECTORY [ $dir2 ] NOT EXIST"
exit 1
fi
# copy all file names in to a text file
ls $dir1 > dir1.txt
ls $dir2 > dir2.txt
mkdir TargetDir
for FileFromDir1 in `cat dir1.txt`
do
flag=0
for FileFromDir2 in `cat dir2.txt`
do
# compare file name is same or not
if [ "$FileFromDir1" = "$FileFromDir2" ]
then
#f file name from directory1 is match to file name of directory2 [ flage is true ]
flag=1
# short both file by time and select first file
record=`ls -lt $dir1/$FileFromDir1 $dir2/$FileFromDir2 | head -n 1`
# copy [ Dir/filename ] of selected file
filePath=`echo $record | cut -d " " -f 8`
# copy selected file to the tageted directory
cp $filePath TargetDir
fi
done
#if file name from directory1 is does NOT match to file name of directory2 [ copy to TargetDir ]
if [ $flag -eq 0 ]
then
cp $dir1/$FileFromDir1 TargetDir
fi
done
# copy rest of file from directory2 to TargetDir
for FileFromDir2 in `cat dir2.txt`
do
flag=0
for FileFromDir1 in `cat dir1.txt`
do
if [ "$FileFromDir2" = "$FileFromDir1" ]
then
flag=1
fi
done
if [ $flag -eq 0 ]
then
cp $dir2/$FileFromDir2 TargetDir
fi
done
echo "Enter First Directory : \c"
read dir1
echo "Enter Second Dirctory : \c"
read dir2
# check inputed name is directory or not
if [ ! -d $dir1 ]
then
echo "DIRECTORY [ $dir1 ] NOT EXIST"
exit 1
fi
if [ ! -d $dir2 ]
then
echo "DIRECTORY [ $dir2 ] NOT EXIST"
exit 1
fi
# copy all file names in to a text file
ls $dir1 > dir1.txt
ls $dir2 > dir2.txt
mkdir TargetDir
for FileFromDir1 in `cat dir1.txt`
do
flag=0
for FileFromDir2 in `cat dir2.txt`
do
# compare file name is same or not
if [ "$FileFromDir1" = "$FileFromDir2" ]
then
#f file name from directory1 is match to file name of directory2 [ flage is true ]
flag=1
# short both file by time and select first file
record=`ls -lt $dir1/$FileFromDir1 $dir2/$FileFromDir2 | head -n 1`
# copy [ Dir/filename ] of selected file
filePath=`echo $record | cut -d " " -f 8`
# copy selected file to the tageted directory
cp $filePath TargetDir
fi
done
#if file name from directory1 is does NOT match to file name of directory2 [ copy to TargetDir ]
if [ $flag -eq 0 ]
then
cp $dir1/$FileFromDir1 TargetDir
fi
done
# copy rest of file from directory2 to TargetDir
for FileFromDir2 in `cat dir2.txt`
do
flag=0
for FileFromDir1 in `cat dir1.txt`
do
if [ "$FileFromDir2" = "$FileFromDir1" ]
then
flag=1
fi
done
if [ $flag -eq 0 ]
then
cp $dir2/$FileFromDir2 TargetDir
fi
done
Labels:
shell script
Subscribe to:
Post Comments (Atom)
Subjects
- WordPress
- Mobile Computing-4649303 Practical Solution
- Android Programming New Syllabus Theory
- PHP LAMP Question Bank
- PHP LAMP Theory
- Step by Step Android Example
- Android Practical
- Android Theory
- Android Question Bank
- Networking FON Practical
- Networking FON Theory
- OS Practical
- OS Theory
- HTML
- JavaScript
- J2EE WTAD Theory
- J2EE WTAD Question Bank
- J2EE WTAD Quick Guide
- J2EE WTAD GTU Papers
- J2EE WTAD Practical
- Python
- JAVA Theory
- JAVA Practical
- MIS
Categories
- Android (55)
- c (11)
- Configure Tomcat7 (2)
- CSS (3)
- Decryption (16)
- Difference (1)
- Encryption (16)
- Error Detection and Correction Techniques (3)
- FON (27)
- Framing Technic (2)
- install Tomcat (2)
- J2EE (29)
- JAVA (13)
- JavaScript (19)
- linux (8)
- OS (17)
- PHP (11)
- Protocol (3)
- SERVER SOCKET PROGRAMING (7)
- Servlet (13)
- shell script (33)
- unix (22)
- WTAD (34)
Blog Archive
-
▼
2012
(79)
-
▼
August
(9)
- Write a script to copy the file system from two di...
- Write a shell script to broadcast a message to a s...
- Write a shell script to find the global complete p...
- write a shell script to fetch the data from a file...
- Write a shell script to accept filename and displa...
- Accept strings and replace a string by another string
- Write a shell script to Accept number and check th...
- Write a shell script to accept the string and chec...
- Write a shell script to accept numbers and perform...
-
▼
August
(9)
Total Pageviews
© BipinRupadiya.com. Powered by Blogger.
0 comments:
Post a Comment