2020-02-21 01:23:21 +00:00
|
|
|
#!/bin/bash
|
|
|
|
|
|
|
|
# Usage:
|
|
|
|
# ./rename-tarball original-name.tar.gz new-name.tar.gz
|
|
|
|
|
|
|
|
set -euo pipefail
|
|
|
|
IFS=$'\n\t'
|
|
|
|
|
|
|
|
positional_args=()
|
|
|
|
while [[ "$#" -gt 0 ]]; do
|
|
|
|
arg="${1}"
|
|
|
|
case "${arg}" in
|
|
|
|
-h|--help)
|
|
|
|
print_usage
|
|
|
|
exit 0
|
|
|
|
;;
|
|
|
|
*)
|
|
|
|
positional_args+=("$1")
|
|
|
|
shift
|
|
|
|
;;
|
|
|
|
esac
|
|
|
|
done
|
|
|
|
|
2021-04-15 03:01:46 +00:00
|
|
|
if [[ -z "${positional_args[0]:-}" ]]; then
|
2020-02-21 01:23:21 +00:00
|
|
|
echo "error: missing original tarball name"
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
2021-04-15 03:01:46 +00:00
|
|
|
original_path=$(readlink -f "${positional_args[0]:-}")
|
|
|
|
original_name=$(basename "$original_path")
|
|
|
|
|
2020-02-21 01:23:21 +00:00
|
|
|
new_name=${positional_args[1]:-}
|
|
|
|
if [[ -z ${new_name} ]]; then
|
|
|
|
echo "error: missing new tarball name"
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
|
|
|
original_name=${original_name/%.tar.gz}
|
|
|
|
new_name=${new_name/.tar.gz}
|
|
|
|
|
|
|
|
echo "Original: ${original_name}.tar.gz"
|
|
|
|
echo "New name: ${new_name}.tar.gz"
|
|
|
|
|
|
|
|
mkdir "temp-${new_name}"
|
|
|
|
pushd "temp-${new_name}"
|
2021-04-15 03:01:46 +00:00
|
|
|
tar xf "${original_path}"
|
|
|
|
mv -- * "${new_name}"
|
2020-02-21 01:23:21 +00:00
|
|
|
tar czf ../"${new_name}.tar.gz" "${new_name}"
|
|
|
|
rm -rf "${new_name}"
|
|
|
|
popd
|
|
|
|
rmdir "temp-${new_name}"
|