Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/bin/bash
- programname=$0
- function usage {
- echo "usage: $programname -l folder1 -r folder2 -d folder3 [-e lrd]"
- echo " -l folder on the left side"
- echo " -r folder on the right side"
- echo " -d destination folder"
- echo " -e exclude l (left side), r (right side) or d (differ)"
- exit 1
- }
- while getopts "l:r:d:e:" option; do
- case "${option}" in
- l) LEFT_FOLDER=${OPTARG};;
- r) RIGHT_FOLDER=${OPTARG};;
- d) DEST_FOLDER=${OPTARG};;
- e) EXCLUDE=$OPTARG;;
- esac
- done
- if [[ -z $LEFT_FOLDER || -z $RIGHT_FOLDER || -z $DEST_FOLDER ]]; then
- usage;
- fi
- diff -rq $LEFT_FOLDER $RIGHT_FOLDER | while read -r line ; do
- set -f;
- linearray=($line);
- set +f;
- if [[ $line = *"differ"* && ! $EXCLUDE = *"d"* ]]; then
- filepath_full=${linearray[3]};
- filepath=${filepath_full%/*};
- dest_filepath=$(echo "$filepath" | sed "s#${RIGHT_FOLDER}/\{0,1\}##g");
- filename_base=${filepath_full##*/};
- if [[ -z $dest_filepath ]]; then
- dest_path="$filename_base";
- else
- dest_path="$dest_filepath/$filename_base";
- fi
- echo "Differ: copy $filepath_full to $DEST_FOLDER/$dest_path";
- mkdir -p "$DEST_FOLDER/$dest_filepath" && cp -R $filepath_full "$DEST_FOLDER/$dest_path" 2>/dev/null;
- fi
- if [[ $line = *"Only in $LEFT_FOLDER"* && ! $EXCLUDE = *"l"* ]]; then
- lpart=${linearray[2]};
- rpart=${linearray[3]};
- filepath_full=${lpart%?}/$rpart;
- dest_filepath=$(echo "${lpart%?}" | sed "s#${LEFT_FOLDER}/\{0,1\}##g");
- if [[ -z $dest_filepath ]]; then
- dest_path="$filename_base";
- else
- dest_path="$dest_filepath/$rpart";
- fi
- echo "Left: copy $filepath_full to $DEST_FOLDER/$dest_path";
- mkdir -p "$DEST_FOLDER/$dest_filepath" && cp -R "$filepath_full" "$DEST_FOLDER/$dest_filepath/$rpart" 2>/dev/null;
- fi
- if [[ $line = *"Only in $RIGHT_FOLDER"* && ! $EXCLUDE = *"r"* ]]; then
- lpart=${linearray[2]};
- rpart=${linearray[3]};
- filepath_full=${lpart%?}/$rpart;
- dest_filepath=$(echo "${lpart%?}" | sed "s#${RIGHT_FOLDER}/\{0,1\}##g");
- if [[ -z $dest_filepath ]]; then
- dest_path="$filename_base";
- else
- dest_path="$dest_filepath/$rpart";
- fi
- echo "Right: copy $filepath_full to $DEST_FOLDER/$dest_path";
- mkdir -p "$DEST_FOLDER/$dest_filepath" && cp -R "$filepath_full" "$DEST_FOLDER/$dest_path" 2>/dev/null;
- fi
- done
Add Comment
Please, Sign In to add comment