• Source
    1. #
    2. # Write a script to copy the file system from two directories to a new directory in such a way that only the
    3. # latest file is copied in case there are common files in both the directories.
    4. #
    5. echo -n "Enter name of the first directory --->"
    6. read d1
    7.  
    8. echo -n "Enter name of the second directory --->"
    9. read d2
    10.  
    11. if [ ! -d $d1 ]
    12. then
    13. echo "Please enter proper first directory"
    14. exit;
    15. elif [ ! -d $d2 ]
    16. then
    17. echo "Please enter proper second directory"
    18. exit;
    19. fi
    20.  
    21. echo -n "Enter name of the destination directory ---> "
    22. read dest
    23.  
    24. if [ -d $dest ]
    25. then
    26. echo "Destination directory is already exist"
    27. else
    28. mkdir $dest
    29. echo "Destination directory is created"
    30. fi
    31.  
    32. #write filenames in text file
    33.  
    34. `ls $d1 > dirfile1.txt`
    35. `ls $d2 > dirfile2.txt`
    36.  
    37. for i in `comm -23 dirfile1.txt dirfile2.txt`
    38. do
    39. `cp $d1/$i $dest`
    40. done
    41. echo "Unique file of first directory is copied to destination"
    42.  
    43. for i in `comm -13 dirfile1.txt dirfile2.txt`
    44. do
    45. `cp $d2/$i $dest`
    46. done
    47. echo "Unique file of second directory is copied to destination"
    48.  
    49. #copying latest common files to destination
    50.  
    51. for i in `comm -12 dirfile1.txt dirfile2.txt`
    52. do
    53. cp `ls -lt $d1/$i $d2/$i | tr -s " " | cut -d " " -f10 | head -n1` $dest
    54.  
    55. done
    56.