Guest User

Untitled

a guest
Mar 20th, 2018
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.51 KB | None | 0 0
  1. #!/bin/bash
  2.  
  3. programname=$0
  4.  
  5. function usage {
  6. echo "usage: $programname -l folder1 -r folder2 -d folder3 [-e lrd]"
  7. echo " -l folder on the left side"
  8. echo " -r folder on the right side"
  9. echo " -d destination folder"
  10. echo " -e exclude l (left side), r (right side) or d (differ)"
  11. exit 1
  12. }
  13.  
  14. while getopts "l:r:d:e:" option; do
  15. case "${option}" in
  16. l) LEFT_FOLDER=${OPTARG};;
  17. r) RIGHT_FOLDER=${OPTARG};;
  18. d) DEST_FOLDER=${OPTARG};;
  19. e) EXCLUDE=$OPTARG;;
  20. esac
  21. done
  22.  
  23. if [[ -z $LEFT_FOLDER || -z $RIGHT_FOLDER || -z $DEST_FOLDER ]]; then
  24. usage;
  25. fi
  26.  
  27. diff -rq $LEFT_FOLDER $RIGHT_FOLDER | while read -r line ; do
  28. set -f;
  29. linearray=($line);
  30. set +f;
  31.  
  32. if [[ $line = *"differ"* && ! $EXCLUDE = *"d"* ]]; then
  33. filepath_full=${linearray[3]};
  34. filepath=${filepath_full%/*};
  35. dest_filepath=$(echo "$filepath" | sed "s#${RIGHT_FOLDER}/\{0,1\}##g");
  36. filename_base=${filepath_full##*/};
  37.  
  38. if [[ -z $dest_filepath ]]; then
  39. dest_path="$filename_base";
  40. else
  41. dest_path="$dest_filepath/$filename_base";
  42. fi
  43.  
  44. echo "Differ: copy $filepath_full to $DEST_FOLDER/$dest_path";
  45. mkdir -p "$DEST_FOLDER/$dest_filepath" && cp -R $filepath_full "$DEST_FOLDER/$dest_path" 2>/dev/null;
  46. fi
  47. if [[ $line = *"Only in $LEFT_FOLDER"* && ! $EXCLUDE = *"l"* ]]; then
  48. lpart=${linearray[2]};
  49. rpart=${linearray[3]};
  50. filepath_full=${lpart%?}/$rpart;
  51. dest_filepath=$(echo "${lpart%?}" | sed "s#${LEFT_FOLDER}/\{0,1\}##g");
  52.  
  53. if [[ -z $dest_filepath ]]; then
  54. dest_path="$filename_base";
  55. else
  56. dest_path="$dest_filepath/$rpart";
  57. fi
  58.  
  59. echo "Left: copy $filepath_full to $DEST_FOLDER/$dest_path";
  60. mkdir -p "$DEST_FOLDER/$dest_filepath" && cp -R "$filepath_full" "$DEST_FOLDER/$dest_filepath/$rpart" 2>/dev/null;
  61. fi
  62. if [[ $line = *"Only in $RIGHT_FOLDER"* && ! $EXCLUDE = *"r"* ]]; then
  63. lpart=${linearray[2]};
  64. rpart=${linearray[3]};
  65. filepath_full=${lpart%?}/$rpart;
  66. dest_filepath=$(echo "${lpart%?}" | sed "s#${RIGHT_FOLDER}/\{0,1\}##g");
  67.  
  68. if [[ -z $dest_filepath ]]; then
  69. dest_path="$filename_base";
  70. else
  71. dest_path="$dest_filepath/$rpart";
  72. fi
  73.  
  74. echo "Right: copy $filepath_full to $DEST_FOLDER/$dest_path";
  75. mkdir -p "$DEST_FOLDER/$dest_filepath" && cp -R "$filepath_full" "$DEST_FOLDER/$dest_path" 2>/dev/null;
  76. fi
  77. done
Add Comment
Please, Sign In to add comment