Skip to content

Instantly share code, notes, and snippets.

@ggrrll
Created October 19, 2023 13:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ggrrll/7ab0107182c30e9154dd3e7d6e48b62a to your computer and use it in GitHub Desktop.
Save ggrrll/7ab0107182c30e9154dd3e7d6e48b62a to your computer and use it in GitHub Desktop.
compare files in two folders
#!/bin/bash
# Argument passing and checking
while getopts 't:r:' flag; do
case "${flag}" in
t) TARGETDIR="${OPTARG}" ;;
r) REFDIR="${OPTARG}" ;;
*)
echo "usage: $0 [-t] [-r]" >&2
exit 1
;;
esac
done
if [ -z "$TARGETDIR" ] || [ -z "$REFDIR" ]; then
echo "Missing required paths"
exit 1
fi
ref_list=()
for f in $(find "$REFDIR" -type f); do
base_name=$(basename "$f")
ref_list+=("$base_name")
done
target_list=()
for f in $(find "$TARGETDIR" -type f); do
base_name=$(basename "$f")
target_list+=("$base_name")
done
# sort lists
sorted_ref=$(echo "${ref_list[@]}" | tr ' ' '\n' | sort | uniq)
sorted_target=$(echo "${target_list[@]}" | tr ' ' '\n' | sort | uniq)
# actual test
echo -e "\nprinting files from $REFDIR missing in $TARGETDIR\n"
unique_to_list1=$(comm -23 <(echo "${sorted_ref[@]}") <(echo "${sorted_target[@]}"))
echo "$unique_to_list1"
echo -e "\n(if nothing was printed, then all files from $REFDIR are present in $TARGETDIR)"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment