I'll help you create a script to accomplish this task. Here's a comprehensive bash script that handles all your requirements:
#!/bin/bash
# Set source and destination directories
SOURCE_DIR="/root/docs/repo_example"
DEST_DIR="/root/docs/results"
# Create destination directory if it doesn't exist
mkdir -p "$DEST_DIR"
# Create temporary directory for tracking files
TEMP_DIR=$(mktemp -d)
trap "rm -rf $TEMP_DIR" EXIT
# Function to check if a file matches our criteria
matches_criteria() {
local file="$1"
local basename=$(basename "$file")
# Check if it ends with .en.mdx
if [[ "$basename" =~ \.en\.mdx$ ]]; then
return 0
fi
# Check if it ends with .mdx but NOT with .[two_letters].mdx
if [[ "$basename" =~ \.mdx$ ]] && ! [[ "$basename" =~ \.[a-z]{2}\.mdx$ ]]; then
return 0
fi
return 1
}
# Function to generate unique filename
generate_unique_filename() {
local source_file="$1"
local dest_dir="$2"
local basename=$(basename "$source_file")
local parent_dir=$(basename "$(dirname "$source_file")")
local dest_file="$dest_dir/$basename"
# If destination doesn't exist, use original name
if [ ! -e "$dest_file" ]; then
echo "$dest_file"
return
fi
# Check if files are identical
if cmp -s "$source_file" "$dest_file" 2>/dev/null; then
echo "SKIP"
return
fi
# Files are different, need to rename
# Try parent directory prefix first
local new_name="${parent_dir}_${basename}"
dest_file="$dest_dir/$new_name"
# If that also exists, add a counter
local counter=1
while [ -e "$dest_file" ]; do
if cmp -s "$source_file" "$dest_file" 2>/dev/null; then
echo "SKIP"
return
fi
new_name="${parent_dir}_${counter}_${basename}"
dest_file="$dest_dir/$new_name"
((counter++))
done
echo "$dest_file"
}
# Main processing
echo "Starting file search and copy operation..."
echo "Source: $SOURCE_DIR"
echo "Destination: $DEST_DIR"
echo
# Find all .mdx files and process them
find "$SOURCE_DIR" -type f -name "*.mdx" | while read -r file; do
if matches_criteria "$file"; then
dest_file=$(generate_unique_filename "$file" "$DEST_DIR")
if [ "$dest_file" = "SKIP" ]; then
echo "Skipping (identical file already exists): $file"
else
echo "Copying: $file -> $dest_file"
cp "$file" "$dest_file"
fi
fi
done
echo
echo "Operation completed!"
echo "Files copied to: $DEST_DIR"
# Optional: Show summary
echo
echo "Summary:"
echo "Total files in destination: $(find "$DEST_DIR" -type f -name "*.mdx" | wc -l)"
Alternative Approach with Better Conflict Resolution
Here's an enhanced version that provides more sophisticated conflict handling:
#!/bin/bash
# Set source and destination directories
SOURCE_DIR="/root/docs/repo_example"
DEST_DIR="/root/docs/results"
# Create destination directory if it doesn't exist
mkdir -p "$DEST_DIR"
# Function to check if a file matches our criteria
matches_criteria() {
local file="$1"
local basename=$(basename "$file")
# Check if it ends with .en.mdx
if [[ "$basename" =~ \.en\.mdx$ ]]; then
return 0
fi
# Check if it ends with .mdx but NOT with .[two_letters].mdx
if [[ "$basename" =~ \.mdx$ ]] && ! [[ "$basename" =~ \.[a-z]{2}\.mdx$ ]]; then
return 0
fi
return 1
}
# Function to get relative path from source directory
get_relative_path() {
local file="$1"
local source="$2"
echo "${file#$source/}"
}
# Function to generate unique filename using full path hash
generate_unique_filename_with_hash() {
local source_file="$1"
local dest_dir="$2"
local source_root="$3"
local basename=$(basename "$source_file")
local name_without_ext="${basename%.*}"
local extension="${basename##*.}"
local relative_path=$(get_relative_path "$source_file" "$source_root")
local dest_file="$dest_dir/$basename"
# If destination doesn't exist, use original name
if [ ! -e "$dest_file" ]; then
echo "$dest_file"
return
fi
# Check if files are identical
if cmp -s "$source_file" "$dest_file" 2>/dev/null; then
echo "SKIP"
return
fi
# Generate a short hash from the relative path
local path_hash=$(echo -n "$relative_path" | md5sum | cut -c1-6)
local new_name="${name_without_ext}_${path_hash}.${extension}"
dest_file="$dest_dir/$new_name"
echo "$dest_file"
}
# Create a log file for tracking operations
LOG_FILE="$DEST_DIR/copy_log.txt"
echo "File Copy Log - $(date)" > "$LOG_FILE"
echo "=========================" >> "$LOG_FILE"
echo >> "$LOG_FILE"
# Main processing with enhanced logging
echo "Starting file search and copy operation..."
echo "Source: $SOURCE_DIR"
echo "Destination: $DEST_DIR"
echo
# Find all .mdx files and process them
find "$SOURCE_DIR" -type f -name "*.mdx" | sort | while read -r file; do
if matches_criteria "$file"; then
dest_file=$(generate_unique_filename_with_hash "$file" "$DEST_DIR" "$SOURCE_DIR")
if [ "$dest_file" = "SKIP" ]; then
echo "Skipping (identical file already exists): $file"
echo "SKIP: $file (identical)" >> "$LOG_FILE"
else
echo "Copying: $file -> $(basename "$dest_file")"
cp "$file" "$dest_file"
echo "COPY: $file -> $dest_file" >> "$LOG_FILE"
fi
fi
done
echo >> "$LOG_FILE"
echo "Operation completed at $(date)" >> "$LOG_FILE"
echo
echo "Operation completed!"
echo "Files copied to: $DEST_DIR"
echo "Log file created at: $LOG_FILE"
# Show summary
echo
echo "Summary:"
total_files=$(find "$DEST_DIR" -type f -name "*.mdx" | wc -l)
echo "Total files in destination: $total_files"