Skip to content

Instantly share code, notes, and snippets.

@dumrauf
Created December 20, 2019 18:10
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 dumrauf/ac3ebd2fac5583e31fd506c9ed046f3c to your computer and use it in GitHub Desktop.
Save dumrauf/ac3ebd2fac5583e31fd506c9ed046f3c to your computer and use it in GitHub Desktop.
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