#!/bin/bash # Script to remove macOS hidden files (.DS_Store, ._*) recursively # Usage: ./remove-mac-files.sh /path/to/directory # Usage: ./remove-mac-files.sh . # Works as Nemo script with selected folders # Colors for output RED='\033[0;31m' GREEN='\033[0;32m' YELLOW='\033[1;33m' BLUE='\033[0;34m' NC='\033[0m' # No Color # Main function main() { local directories=("$@") # Get directories to process local target_dirs=() # If we have directory arguments, process them if [ ${#directories[@]} -gt 0 ]; then # Process directory arguments and check if they exist for dir in "${directories[@]}"; do if [ -d "$dir" ]; then target_dirs+=("$dir") else echo -e "${RED}Error: Directory '$dir' does not exist.${NC}" >&2 fi done else echo -e "${RED}Error: No directory specified.${NC}" echo -e "${YELLOW}Usage from terminal: $0 /path/to/directory${NC}" echo -e "${YELLOW}Usage from terminal: $0 .${NC}" echo -e "${YELLOW}Or select folders in Nemo and run as script${NC}" exit 1 fi if [ ${#target_dirs[@]} -eq 0 ]; then echo -e "${YELLOW}No valid directories found to process.${NC}" exit 0 fi local total_dirs=${#target_dirs[@]} # Detect if running from Nemo (file manager) or terminal local use_gui=false # Use GUI if we're likely running from Nemo and zenity is available if [ -n "$NEMO_SCRIPT_SELECTED_FILE_PATHS" ] && command -v zenity &> /dev/null; then use_gui=true fi # Show initial info only for terminal if [ "$use_gui" = false ]; then echo -e "${GREEN}macOS hidden files cleanup started${NC}" echo -e "${BLUE}Directories to process: ${total_dirs}${NC}" echo "" fi # Process each directory with appropriate progress display local count=0 local total_removed=0 if [ "$use_gui" = true ]; then # Create temporary files for counting local count_file=$(mktemp) local dirs_file=$(mktemp) echo "0" > "$count_file" echo "0" > "$dirs_file" # Use zenity progress bar for GUI ( local local_count=0 for dir in "${target_dirs[@]}"; do local_count=$((local_count + 1)) dir_name=$(basename "$dir") echo "# Cleaning $dir_name... ($local_count/$total_dirs)" # Count and remove macOS hidden files local removed_count=0 # Find and count .DS_Store files while IFS= read -r -d '' file; do rm -f "$file" && removed_count=$((removed_count + 1)) done < <(find "$dir" -type f -name ".DS_Store" -print0 2>/dev/null) # Find and count ._* files while IFS= read -r -d '' file; do rm -f "$file" && removed_count=$((removed_count + 1)) done < <(find "$dir" -type f -name "._*" -print0 2>/dev/null) # Update counts in temporary files local current_total=$(cat "$count_file") echo "$((current_total + removed_count))" > "$count_file" echo "$local_count" > "$dirs_file" echo "$((local_count * 100 / total_dirs))" done ) | zenity --progress --title="macOS Files Cleanup" --text="Initializing..." --percentage=0 --auto-close # Read final counts from temporary files total_removed=$(cat "$count_file") count=$(cat "$dirs_file") rm -f "$count_file" "$dirs_file" # Check if user cancelled if [ $? -ne 0 ]; then zenity --error --text="Cleanup was cancelled." exit 1 fi else # Use terminal progress bar for dir in "${target_dirs[@]}"; do count=$((count + 1)) dir_name=$(basename "$dir") # Show terminal progress local percentage=$((count * 100 / total_dirs)) printf "\r${BLUE}[%3d%%] Cleaning $dir_name ($count/$total_dirs)${NC}" "$percentage" # Count and remove macOS hidden files local removed_count=0 # Find and count .DS_Store files while IFS= read -r -d '' file; do rm -f "$file" && removed_count=$((removed_count + 1)) done < <(find "$dir" -type f -name ".DS_Store" -print0 2>/dev/null) # Find and count ._* files while IFS= read -r -d '' file; do rm -f "$file" && removed_count=$((removed_count + 1)) done < <(find "$dir" -type f -name "._*" -print0 2>/dev/null) total_removed=$((total_removed + removed_count)) done echo "" # New line after progress fi # Show final report if [ "$use_gui" = true ]; then # Show report with zenity local report_message="✓ Cleanup completed!\n\n" report_message+="Directories processed: ${count}\n" report_message+="Files removed: ${total_removed}" zenity --info --title="macOS Files Cleanup Complete" --text="$report_message" else # Show report in terminal echo "" echo -e "${GREEN}✓ Cleanup completed!${NC}" echo "" echo -e "${BLUE}Results:${NC}" echo -e " Directories processed: ${count}" echo -e " Files removed: ${total_removed}" fi } # Show help if requested if [[ "$1" == "-h" || "$1" == "--help" ]]; then echo -e "${GREEN}Script to remove macOS hidden files recursively${NC}" echo "" echo -e "${YELLOW}Usage:${NC}" echo " $0 /path/to/directory" echo " $0 ." echo " Or select folders in Nemo file manager and run as script" echo "" echo -e "${YELLOW}Parameters:${NC}" echo " directory Directory to clean (can specify multiple)" echo "" echo -e "${YELLOW}Examples:${NC}" echo " $0 /home/user/Downloads # Clean Downloads folder" echo " $0 . # Clean current directory" echo " $0 /media/usb /home/shared # Clean multiple directories" echo "" echo -e "${BLUE}Note: Removes .DS_Store and ._* files recursively${NC}" echo -e "${BLUE} When used as Nemo script, progress is shown graphically${NC}" echo -e "${BLUE} No external dependencies required${NC}" exit 0 fi # Execute main main "$@"