Skip to content

Instantly share code, notes, and snippets.

@naranyala
Last active August 8, 2023 03:12
Show Gist options
  • Save naranyala/84b978cb50688d32e05261e9213db9d4 to your computer and use it in GitHub Desktop.
Save naranyala/84b978cb50688d32e05261e9213db9d4 to your computer and use it in GitHub Desktop.
#!/bin/bash
# Check if the compressed file argument is provided
if [ -z "$1" ]; then
echo "Please provide the path to the compressed file as the first argument."
exit 1
fi
# Verify if the compressed file exists
compressed_file="$1"
if [ ! -f "$compressed_file" ]; then
echo "File not found!"
exit 1
fi
# Extract the default filename or prompt for the extraction directory
if [ -z "$2" ]; then
default_dir=$(basename "$compressed_file" | sed 's/\.tar\.gz$//; s/\.tgz$//')
read -p "Enter the extraction directory path (default: $default_dir): " extraction_dir
extraction_dir="${extraction_dir:-$default_dir}"
else
extraction_dir="$2"
fi
# Check if the directory exists or create it
if [ ! -d "$extraction_dir" ]; then
mkdir -p "$extraction_dir"
fi
# Extract the file to the specified directory
tar -xf "$compressed_file" --strip-components=1 -C "$extraction_dir"
echo "Extraction complete!"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment