• Source
    1. #
    2. #Write a script to compare identically named files in two different directories and if they are same, copy
    3. #one of them in a third directory.
    4. #
    5. clear
    6. echo -n "Enter first directory name :"
    7. read d1
    8. echo -n "Enter second directory name :"
    9. read d2
    10. if [ ! -d $d1 ]
    11. then
    12. echo "Enter proper first directory"
    13. elif [ ! -d $d2 ]
    14. then
    15. echo "Enter proper second directory"
    16. else
    17. echo -n "Enter destination directory :"
    18. read destination
    19. if [ ! -d $destination ]
    20. then
    21. $(mkdir $destination)
    22. echo "Destination directory : $destination created"
    23. fi
    24. $(ls $d1 > "firstdirectory.txt")
    25. $(ls $d2 > "seconddirectory.txt")
    26. count=0
    27. for i in `comm -12 firstdirectory.txt seconddirectory.txt`
    28. do
    29. filename=$(ls $d1/$i | tr -s " " | cut -d " " -f8)
    30. a=$(diff $d1/$i $d2/$i)
    31. if [ $? -eq 0 ]
    32. then
    33. $(cp $filename $destination)
    34. count=$(echo $count + 1 | bc -l)
    35. fi
    36. done
    37. echo "Total files copied : $count"
    38. fi