Skip to content

Instantly share code, notes, and snippets.

@vinesmsuic
Created November 14, 2023 00:21
Show Gist options
  • Save vinesmsuic/7d66c7bfed3c360d2d99d26bb6a0682a to your computer and use it in GitHub Desktop.
Save vinesmsuic/7d66c7bfed3c360d2d99d26bb6a0682a to your computer and use it in GitHub Desktop.
Only keep multiple folders with same file name. Align is the reference folder.
#!/bin/bash
# Define the path to your root folder
rootFolder="."
# Define the path to your input folder (folderA)
folderA="$rootFolder/align"
# Function to delete excess files and print file count
delete_excess_files () {
local fileCount=0
for file in "$1"/*; do
# Increment file count
((fileCount++))
filename=$(basename "$file")
if [ ! -f "$folderA/$filename" ]; then
echo "Deleting $file"
rm "$file"
((fileCount--))
fi
done
echo "Number of files remaining in $(basename "$1"): $fileCount"
}
# Loop through each folder in the root folder
for folder in "$rootFolder"/*; do
# Check if it's a directory and not the input folder
if [ -d "$folder" ] && [ "$(basename "$folder")" != "input" ]; then
echo "Processing $folder"
delete_excess_files "$folder"
fi
done
echo "Processing complete."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment