# Interactive Multi-Extension File Deleter
=> /ru/delete.gmi 🇷🇺 Read in Russian

A robust Bash function designed for Fedora and Raspberry Pi power users to safely clean up directories. It supports searching by multiple extensions at once, features a toggle for hidden directories, and includes a mandatory confirmation step to prevent accidental data loss.

Key Highlight: The code includes a comprehensive English header—a "gift to your future self." Even if you open this script six months from now, you won't have to painfully wonder what it does or how to use it; the documentation is baked right into the file.


```bash
# ==============================================================================
# COMMAND: delete
# DESCRIPTION: Search and interactively delete files by a list of extensions.
#
# USAGE:
#   delete <ext1> [ext2] [ext3] ...
#   Example: delete asc txt log
#
# FEATURES:
#   1. Multi-extension support: Handles multiple extensions in one go.
#   2. Hidden files toggle: Optional search in hidden folders (e.g., .git, .cache).
#   3. Safety preview: Displays the full list and file count before proceeding.
#   4. Confirmation: Requires explicit 'y' or 'yes' to execute permanent deletion.
#
# SYSTEM NOTE:
#   Uses 'find' with the '-delete' flag for efficiency. Deletion is permanent.
# ==============================================================================

delete() {
    # Check if at least one extension is provided
    if [ -z "$1" ]; then
        echo "⚠️  Error: Please specify at least one extension. Example: delete jpg png"
        return 1
    fi

    # Toggle for hidden files and directories
    read -p "📁 Search in hidden folders and files (.*)? (y/N): " hidden
    
    # Initialize find arguments as an array to handle spaces and special characters
    local find_args=(".")
    
    # If user chooses NOT to search hidden paths
    if [[ ! $hidden == [yY] && ! $hidden == [yY][eE][sS] ]]; then
        find_args+=("-not" "-path" "*/.*")
    fi

    # Start the file type and extension filter
    find_args+=("-type" "f" "(")

    local first=true
    for ext in "$@"; do
        if [ "$first" = false ]; then
            find_args+=("-o")
        fi
        find_args+=("-name" "*.$ext")
        first=false
    done
    
    find_args+=(")")

    echo "🔍 Searching for files with extensions: $@ ..."
    
    # Execute search and store results
    # Quoting the array ensures complex paths are handled correctly
    found_files=$(find "${find_args[@]}")

    # Handle empty results
    if [ -z "$found_files" ]; then
        echo "No files with extension(s) .$@ found."
        return 0
    fi

    # Display the list of files to be deleted
    echo "$found_files"
    echo "----------------------------------------"
    count=$(echo "$found_files" | wc -l)
    echo "Total files found: $count"

    # Final safety confirmation
    read -p "🗑️  Delete these files PERMANENTLY? (y/N): " confirm
    if [[ $confirm == [yY] || $confirm == [yY][eE][sS] ]]; then
        find "${find_args[@]}" -delete
        echo "✅ Done. All specified files have been deleted."
    else
        echo "❌ Cancelled. No files were deleted."
    fi
}
```
=> index.gmi Back to Bash Section

=> / Home
