# clone
cd ~/Downloads
git clone https://gist.github.com/d76520b1bfa66713a5e27ead5e2c4e93.git archive
# create local bin directory
mkdir -p ~/.local/bin
# make executable
chmod +x archive.sh
# create symlink
ln -s $(pwd)/archive.sh $HOME/.local/bin/archive
# symlink alias
ln -s $(pwd)/archive.sh $HOME/.local/bin/all
# ! Be sure to add $HOME/.local/bin/ to $PATH in ~/.bashrc OR ~/.zshrc
# display help
archive <-h|--help>
# run in working directory
archive <-d|-c>
# pass an argument for different directory
archive <-d|-c> </Volume/data>
Last active
October 19, 2023 05:44
-
-
Save pythoninthegrass/d76520b1bfa66713a5e27ead5e2c4e93 to your computer and use it in GitHub Desktop.
Decompress/compress all directories ineither a given or current directory.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env bash | |
| # shellcheck disable=SC2035,SC2046,SC2086,SC2155,SC2317 | |
| # set IFS to only split on newlines and tabs | |
| export IFS="$(printf '\n\t')" | |
| # trap function | |
| trap_card() { | |
| echo "Caught SIGINT/SIGTSTP/SIGTERM. Cleaning up..." | |
| unset IFS | |
| exit 1 | |
| } | |
| trap trap_card SIGINT SIGTSTP SIGTERM # EXIT/SIGINT/SIGTSTP/SIGTERM | |
| # help function showing description | |
| help() { | |
| cat <<- 'DESCRIPTION' >&2 | |
| Decompress/compress all directories in either a given or current directory. | |
| USAGE | |
| ./archive.sh [OPTION] | |
| OPTIONS | |
| -d, --decompress Decompress all directories in current directory. | |
| -c, --compress Compress all directories in current directory. | |
| -h, --help Show this help message. | |
| DESCRIPTION | |
| } | |
| # get directory where script was called from (i.e., physical current working directory) | |
| work_dir=$(pwd -P) | |
| # get all directories in directory | |
| if [ $# -le 1 ]; then | |
| dirs=$(ls -d ${work_dir}/*/ 2>/dev/null) | |
| else | |
| dirs=$(ls -d "$2"/*/ 2>/dev/null) | |
| fi | |
| # get all zip files in directory | |
| if [ $# -le 1 ]; then | |
| zips=$(ls ${work_dir}/*.zip 2>/dev/null) | |
| else | |
| zips=$(ls "$2"/*.zip 2>/dev/null) | |
| fi | |
| # verify if zip or directory is present | |
| check_file_type() { | |
| if [ -n "$dirs" ] || [ -n "$zips" ]; then | |
| return 0 | |
| else | |
| echo "No directories or zip files found." >&2 | |
| return 1 | |
| fi | |
| } | |
| # user input | |
| confirm() { | |
| read -r -p "$1" response | |
| case "$response" in | |
| [yY][eE][sS]|[yY]) | |
| true | |
| ;; | |
| *) | |
| false | |
| ;; | |
| esac | |
| } | |
| # unzip all directories with names used as suffix before zip extension | |
| decompress() { | |
| check_file_type | |
| if [ -n "$zips" ]; then | |
| prompt=$(echo -e "Decompress all zip files?:\n$(printf '%s\n' $(basename $zips))\n\n[y/N] ") | |
| if ! confirm "$prompt"; then | |
| exit 0 | |
| fi | |
| fi | |
| for file in $zips; do | |
| dir=${file%.zip} | |
| mkdir -p "$dir" | |
| unzip "$file" -d "$dir" | |
| done | |
| } | |
| # zip all directories with names used as suffix before zip extension | |
| compress() { | |
| check_file_type | |
| if [ -n "$dirs" ]; then | |
| prompt=$(echo -e "Compress all directories?:\n$(printf '%s\n' $(basename $dirs))\n\n[y/N] ") | |
| if ! confirm "$prompt"; then | |
| exit 0 | |
| fi | |
| fi | |
| for dir in $dirs; do | |
| dir=${dir%*/} | |
| zip -r "${dir}.zip" "$dir" -j | |
| done | |
| } | |
| # case statement for options | |
| case "$1" in | |
| -d|--decompress) | |
| decompress | |
| ;; | |
| -c|--compress) | |
| compress | |
| ;; | |
| -h|--help) | |
| help | |
| exit 0 | |
| ;; | |
| *) | |
| help | |
| exit 1 | |
| ;; | |
| esac | |
| unset IFS | |
| exit 0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment