Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Find out if two directories contain the same files or where they differ; see https://www.how-hard-can-it.be/basic-directories-diff/ for details
#!/bin/bash
# Input parameter validation
if [ "$#" -lt 2 ]; then
script_name=$(basename "$0")
echo "Usage: ${script_name} <directory-one> <directory-two>"
echo "Example: ${script_name} one/ two/"
exit -1
fi
# Input parameter assignment
directory_one=$1
directory_two=$2
# Get the contents of the two directories
contents_directory_one=$(cd "${directory_one}" && find . | sort)
contents_directory_two=$(cd "${directory_two}" && find . | sort)
# Diff the contents of the two directories
difference=$(diff -u <(echo "${contents_directory_one}") <(echo "${contents_directory_two}"))
# Provide some visual feedback to the user
if [ -z "${difference}"]
then
echo "Both directories are identical."
else
echo "The directories differ!"
echo "${difference}"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.