Created
July 11, 2023 08:32
-
-
Save phillippbertram/45a21f141670cf435992c59e0189f763 to your computer and use it in GitHub Desktop.
find duplicate files by name
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env zsh | |
# Function to find duplicate files in a directory | |
find_duplicate_files() { | |
# Create an associative array to store filenames | |
declare -A filenames | |
duplicate_found=false | |
# Find all files in the given directory and its subdirectories | |
find "$1" -type f | while IFS= read -r file; do | |
# Get the filename from the path | |
filename=$(basename "$file") | |
# Check if the filename already exists in the array | |
if [[ -n "${filenames[$filename]}" ]]; then | |
# Duplicate file found | |
if [[ "${filenames[$filename]}" != "DUPLICATE" ]]; then | |
# Print the duplicate filenames | |
echo "Duplicate file found: $filename" | |
filenames[$filename]="DUPLICATE" | |
duplicate_found=true | |
fi | |
echo " - $file" | |
else | |
# Store the filename in the array | |
filenames[$filename]="$file" | |
fi | |
done | |
# Check if any duplicate files were found | |
if [[ $duplicate_found = false ]]; then | |
echo "No duplicate files found." | |
fi | |
} | |
# Check if a directory is provided as an argument | |
if [[ -z "$1" ]]; then | |
echo "Please provide a directory as an argument." | |
exit 1 | |
fi | |
# Check if the directory exists | |
if [[ ! -d "$1" ]]; then | |
echo "Directory '$1' not found." | |
exit 1 | |
fi | |
# Find duplicate files in the given directory | |
find_duplicate_files "$1" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
make it executable using
chmod +x find_duplicates.sh
, and run it with the desired directory as the argument:This revised script utilizes the find command to search for files recursively, ensuring that duplicate files within nested directories are also identified and displayed.