#!/bin/bash # Ensure pigz is installed if ! command -v pigz &> /dev/null; then echo "Error: pigz is not installed. Please install it and try again." exit 1 fi # Defined the root directory of the whole dataset ROOT_DIR="/ssd2/TzuYu/data" # Function to display usage usage() { echo "Usage: $0 [--method METHOD --config CONFIG] | [--dir-path DIR_PATH] | [--all]" exit 1 } # Function to zip a single directory zip_config() { local method=$1 local config=$2 local dir_path="$ROOT_DIR/$method/$config" local output_path="./data/$method/$config/$config.tar.gz" # Ensure the output directory exists mkdir -p "$(dirname "$output_path")" # Create the tar.gz file echo "Zipping $dir_path to $output_path" tar -cv --use-compress-program=pigz -f "$output_path" -C "$dir_path" . if [[ $? -ne 0 ]]; then echo "Error: Failed to create tar.gz for $dir_path" exit 1 fi } # Parse arguments while [[ "$#" -gt 0 ]]; do case $1 in --help) usage ;; --h) usage ;; --all) ALL="true" ;; --method) METHOD="$2"; shift ;; --config) CONFIG="$2"; shift ;; --dir-path) DIR_PATH="$2"; shift ;; *) echo "Unknown parameter: $1"; usage ;; esac shift done # zipping all folders if [[ "$ALL" == "true" ]]; then # Iterate through all methods and configs for method_dir in "$ROOT_DIR"/*; do if [[ -d "$method_dir" && "$(basename "$method_dir")" != "Origin" ]]; then method=$(basename "$method_dir") for config_dir in "$method_dir"/config*; do if [[ -d "$config_dir" ]]; then config=$(basename "$config_dir") zip_config "$method" "$config" fi done fi done if [ ! -e "./data/Origin/Origin.tar.gz" ]; then # Ensure the output directory exists mkdir -p "./data/Origin" # Create the tar.gz file echo "Zipping $ROOT_DIR/Origin to ./data/Origin/Origin.tar.gz" tar -cv --use-compress-program=pigz -f "./data/Origin/Origin.tar.gz" -C "$ROOT_DIR/Origin" . if [[ $? -ne 0 ]]; then echo "Error: Failed to create tar.gz for $ROOT_DIR/Origin" exit 1 fi fi echo "All configurations have been zipped successfully." exit 0 fi SPECIFIED="false" # Validate arguments if [[ -n "$METHOD" && -n "$CONFIG" ]]; then DIR_PATH="$ROOT_DIR/$METHOD/config$CONFIG" SPECIFIED="true" elif [[ -z "$DIR_PATH" ]]; then echo "Error: Either --method and --config or --dir-path must be specified." usage fi # Check if the directory exists if [[ ! -d "$DIR_PATH" ]]; then echo "Error: Directory $DIR_PATH does not exist." exit 1 fi # Extract method and config index from the directory path METHOD=$(basename "$(dirname "$DIR_PATH")") CONFIG=$(basename "$DIR_PATH") # Define the output tar file name # If method and config path specified, saved to the data hirarchy # Otherwise, saved to the repo root directory if [[ "$SPECIFIED" == "false" ]]; then OUTPUT_TAR="./$CONFIG.tar.gz" echo "Zipping $DIR_PATH to $OUTPUT_TAR" tar -cv --use-compress-program=pigz -f "$OUTPUT_TAR" -C "$DIR_PATH" . if [[ $? -ne 0 ]]; then echo "Error: Failed to create tar.gz for $dir_path" exit 1 fi else # Create the tar.gz file zip_config "$METHOD" config"$CONFIG" fi echo "Directory zipped successfully to: $OUTPUT_TAR"