loader image

Visual File Move Script: One-at-a-Time Moving with Space Checks

What makes us different from other similar websites? Forums Tech Visual File Move Script: One-at-a-Time Moving with Space Checks

Viewing 1 post (of 1 total)
  • Author
    Posts
  • #8476
    thumbtak
    Moderator
    #!/bin/bash
    
    draw_header() {
    clear
    echo -e "\e[36m"
    cat << 'EOF'
    ░████████╗░█████╗░██╗░░██╗░██████╗░██████╗██╗░░██╗░█████╗░░██████╗██╗░░██╗
    ░╚══██╔══╝██╔══██╗██║░██╔╝██╔════╝██╔════╝██║░░██║██╔══██╗██╔════╝██║░██╔╝
    ░░░░██║░░░███████║█████═╝░╚█████╗░╚█████╗░███████║███████║██║░░░░░█████═╝░
    ░░░░██║░░░██╔══██║██╔═██╗░░╚═══██╗░╚═══██╗██╔══██║██╔══██║██║░░░░░██╔═██╗░
    ░░░░██║░░░██║░░██║██║░╚██╗██████╔╝██████╔╝██║░░██║██║░░██║╚██████╗██║░╚██╗
    ░░░░╚═╝░░░╚═╝░░╚═╝╚═╝░░╚═╝╚═════╝░╚═════╝░╚═╝░░╚═╝╚═╝░░╚═╝░╚═════╝╚═╝░░╚═╝
    ░░░░░░░░░░░░░░░░░░░░░░░░░░░░.com░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
    EOF
    echo -e "\e[33m"
    echo "========================================================"
    echo " CREATED BY: thumbtak"
    echo " PURPOSE: Securely move files one-by-one with"
    echo " real-time space checks and progress bar."
    echo "========================================================"
    echo -e "\e[34m"
    cat << 'EOF'
    ░███╗░░░███╗░█████╗░██╗░░░██╗███████╗
    ░████╗░████║██╔══██╗██║░░░██║██╔════╝
    ░██╔████╔██║██║░░██║╚██╗░██╔╝█████╗░░
    ░██║╚██╔╝██║██║░░██║░╚████╔╝░██╔══╝░░
    ░██║░╚═╝░██║╚█████╔╝░░╚██╔╝░░███████╗
    ░╚═╝░░░░░╚═╝░╚════╝░░░░╚═╝░░░╚══════╝
    EOF
    echo -e "\e[0m"
    }
    
    draw_progress() {
    local current=$1
    local total=$2
    local percent=$(( (current * 100) / total ))
    local filled=$(( percent / 2 ))
    local empty=$(( 50 - filled ))
    printf "\rMoving: [\e[44m%-50s\e[0m] %d%% (%d/%d)" "$(printf '#%.0s' $(seq 1 $filled))" "$percent" "$current" "$total"
    }
    
    draw_header
    
    echo -e "\n\e[1mEnter the SOURCE folder path:\e[0m"
    read -r -e SOURCE_DIR
    
    echo -e "\mEnter the DESTINATION folder path:\e[0m"
    read -r -e DEST_DIR
    
    SOURCE_DIR="${SOURCE_DIR%/}"
    DEST_DIR="${DEST_DIR%/}"
    
    if [[ ! -d "$SOURCE_DIR" ]]; then
    echo -e "\n\e[31m[!] Error: Source directory does not exist.\e[0m"
    exit 1
    fi
    
    if [[ ! -d "$DEST_DIR" ]]; then
    echo -e "\n\e[33m[?] Destination doesn't exist. Create it? (y/n):\e[0m"
    read -r mk_dest
    if [[ "$mk_dest" == "y" ]]; then
    mkdir -p "$DEST_DIR"
    else
    exit 1
    fi
    fi
    
    mapfile -t files < <(find "$SOURCE_DIR" -maxdepth 1 -type f)
    total_files=${#files[@]}
    
    if [ "$total_files" -eq 0 ]; then
    echo -e "\n\e[33m[!] No files found in source folder.\e[0m"
    exit 0
    fi
    
    echo -e "\n\e[32m--- PRE-MOVE SUMMARY ---\e[0m"
    echo "Files to move: $total_files"
    echo "Source: $SOURCE_DIR"
    echo "Destination: $DEST_DIR"
    echo "------------------------------------------------"
    read -p "Proceed with move? (y/n): " confirm
    [[ "$confirm" != "y" ]] && exit 0
    
    echo -e "\nStarting...\n"
    
    current_count=0
    for file in "${files[@]}"; do
    filename=$(basename "$file")
    
    if [[ "$OSTYPE" == "darwin"* ]]; then
    filesize=$(stat -f%z "$file")
    else
    filesize=$(stat -c%s "$file")
    fi
    
    available_space=$(df --output=avail -B1 "$DEST_DIR" | tail -n 1)
    
    if [ "$filesize" -gt "$available_space" ]; then
    echo -e "\n\n\e[31m[STOPPED] Not enough space for: $filename\e[0m"
    break
    fi
    
    if cp "$file" "$DEST_DIR/"; then
    rm "$file"
    current_count=$((current_count + 1))
    draw_progress "$current_count" "$total_files"
    else
    echo -e "\n\e[31m[!] Failed to copy: $filename\e[0m"
    fi
    done
    
    echo -e "\n\n\e[32m[COMPLETE] $current_count files moved.\e[0m"
Viewing 1 post (of 1 total)
  • You must be logged in to reply to this topic.
TAKs Shack