Created
January 20, 2022 00:59
-
-
Save deanhouseholder/a5e07822a71e0eefcb5ec534e2a8afe2 to your computer and use it in GitHub Desktop.
Bash enhanced extraction of common filetypes
This file contains 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
## Extract function with progress bars where possible | |
# Syntax is: | |
# e [filename] | |
# | |
# Install "pv" and "funzip" packages for progress bars | |
function e() { | |
# Exit the function if the file is not found | |
if [[ ! -f "$1" ]]; then | |
printf "\n\e[31mERROR: Couldn't find file to extract.\e[0m\n" | |
return 1 | |
fi | |
local filename=$(basename $1) | |
local dir=$(pwd) | |
printf "Extract $filename into $dir\n\n" | |
local count=$(\ls | wc -l | awk '{print $1}') | |
if [[ $count -ne 0 ]]; then | |
printf "\e[33mWARNING: The current directory is not empty!\n\n" | |
printf "There are currently [$count] items in this directory.\e[0m\n\n" | |
printf "Would you like to proceed anyway? [y/N]\n" | |
read -sn 1 p | |
if [[ ! $p =~ [yY] ]]; then | |
printf "\nCancelled\n" | |
return 1 | |
fi | |
echo | |
fi | |
case "$1" in | |
*.7z) ext=".7z"; cmd="7z"; options="x"; usepv=0;; | |
*.tar.bz2) ext=".tar.bz2"; cmd="tar"; options="jxvf"; usepv=0;; | |
*.tbz2) ext=".tbz2"; cmd="tar"; options="jxvf"; usepv=0;; | |
*.tar.gz) ext=".tar.gz"; cmd="tar"; options="zxvf"; usepv=0;; | |
*.tgz) ext=".tgz"; cmd="tar"; options="zxvf"; usepv=0;; | |
*.tar) ext=".tar"; cmd="tar"; options="xvf"; usepv=0;; | |
*.rar) ext=".rar"; cmd="unrar"; options="x"; usepv=0;; | |
*.Z) ext=".Z"; cmd="uncompress"; options=""; usepv=0;; | |
*.bz2) ext=".bz2"; cmd="bunzip2"; options=""; usepv=1;; | |
*.gz) ext=".gz"; cmd="gunzip"; options=""; usepv=1;; | |
*.zip) ext=".zip"; cmd="unzip"; options=""; usepv=1;; | |
*) printf "\e[31mError: Cannot determine how to extract '$1'\e[0m\n" && return 1;; | |
esac | |
printf "\e[32mExtracting $filename\e[0m\n" | |
# Check if extraction command is installed/executable | |
[[ -x "$(type -fP fzf)" ]] || printf "\e[31mError: $cmd is not installed\e[0m\n" && return 1 | |
# Check if pv is enabled and installed | |
if [[ $usepv -eq 1 ]] && [[ -x "$(type -fP pv)" ]]; then | |
filesize=$(stat -c '%s' "$1") | |
newfile="${1%%$ext}" | |
# Handle special case for .zip since unzip command doesn't allow piping and funzip doesn't allow multiple files | |
if [[ $ext == ".zip" ]]; then | |
# If the .zip contains 1 file and if funzip and pv are installed we can show a progress bar | |
if [[ $(zipinfo -t "$1" | awk '{print $1}') -eq 1 ]] && [[ -x "$(type -fP funzip)" ]]; then | |
cat "$1" | pv -s $filesize -i 0.1 -D 0 | funzip > "$newfile" | |
else | |
unzip "$1" | |
fi | |
else | |
# Use pv command to show progress bars | |
cat "$1" | pv -s $filesize -i 0.1 -D 0 | $cmd $options > $newfile | |
fi | |
else | |
# Run command | |
$cmd $options "$1" | |
fi | |
printf "\nDone\n\n" | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment