Skip to content

Instantly share code, notes, and snippets.

@tcely
Last active January 17, 2026 15:30
Show Gist options
  • Select an option

  • Save tcely/88a38810ff662e6bcfc530ec37d87397 to your computer and use it in GitHub Desktop.

Select an option

Save tcely/88a38810ff662e6bcfc530ec37d87397 to your computer and use it in GitHub Desktop.
An improvement of the functions shown in: https://youtu.be/DsmKmmKbz_4

I went looking for the source of the original extract function. The oldest version that I managed to find was from:

Which is currently located at: https://github.com/dotgc/dotfiles/blob/adef9b6106635b0b06fad2df760a41e563afc289/.lib/functions.bash#L1-L23

After also finding another repository and a gist from that same GitHub user, I remain unaware of what unrar2dir and unzip2dir actually were.


If anyone knows anything about unrar2dir or unzip2dir, that would be useful information to add.

# Show a directory listing when using 'cd'
cd() {
builtin cd "$@" && \
command ls -lhF --time-style=long-iso --color=auto --ignore=lost+found
}
extract() (
err() {
_rc="${1}" ;
shift ;
printf -- "'%s'" "${1}" ;
shift ;
printf -- ' %s' "$@" ;
printf -- '\n' ;
return "${_rc}" ;
} 1>&2 ;
find_binary() {
for cmd in "$@" ; do
command -v "${cmd}" && return 0 ;
done ;
return 1 ;
} ;
get_dir() {
_awk_program='BEGIN {OFS=FS;} {NF--;} /\.tar$/ {NF--;} {print;}' ;
printf -- '%s\n' "$@" | awk -F . "${_awk_program}" ;
unset -v _awk_program ;
}
if ! [ -f "${1}" ] ; then
err 1 "${1}" 'is not a valid file' ;
exit $? ;
fi ;
_filename="$(basename "${1}")" ;
unset -v supported compress gzip bzip2 xz tar unzip zstd ;
case "${_filename}" in
(*.Z|*.taZ) supported=1 compress=1 ;;
(*.gz|*.tgz|*.taz) supported=1 gzip=1 ;;
(*.bz2|*.tbz2|*.tz2|*.tbz) supported=1 bzip2=1 ;;
(*.xz) supported=1 xz=1 ;;
(*.tar.zst|*.tar.zstd|*.tzst) supported=1 tar=1 zstd=1 ;;
(*.tar) supported=1 tar=1 ;;
(*.zip|*.7z) supported=1 unzip=1 ;;
(*.zst) supported=1 zstd=1 ;;
(*.ace|*.rar) supported=1 ;;
esac ;
if ! [ 1 -eq "${supported:-0}" ] ; then
err 2 "${_filename}" 'cannot be extracted via extract()' ;
exit $? ;
fi ;
_archive="$(realpath "${1}")" ;
_out_dir="$(get_dir "${_filename}")" ;
mkdir -p "${_out_dir}" || err 3 "${_out_dir}" 'directory could not be created' || exit ;
cd "${_out_dir}" || err 4 "${_out_dir}" 'could not change directory' || exit ;
case "${_filename}" in
(*.tar.zst|*.tar.zstd|*.tzst) tar --zstd -xvvpf "${_archive}" ;;
(*.tar.Z|*.taZ|*.tar.gz|*.tgz|*.tar.bz2|*.tbz2|*.tar.xz|*.tar)
tar "-xvvp${compress:+Z}${gzip:+z}${bzip2:+j}${xz:+J}f" "${_archive}"
;;
(*.gz|*.Z) gzip -d "${_archive}" ;;
(*.bz2) "$(find_binary lbzip2 bzip2)" -d "${_archive}" ;;
(*.xz) xz -d "${_archive}" ;;
(*.zst) zstd -d "${_archive}" ;;
(*.zip|*.7z)
# 7zip has a few commands that might be installed
# Fallback to unzip if we found none of them
unset -v _7zip ;
cmd="$(find_binary 7z 7za 7zz)" && _7zip=1 ;
"${cmd:-unzip}" ${_7zip:+x} "${_archive}"
;;
(*.ace) acefile-unace -x "${_archive}" ;;
(*.rar)
unrar-free --version >/dev/null 2>&1 &&
unrar-free -x "${_archive}" ||
unrar x "${_archive}"
;;
esac ;
)
# I didn't find the original implementation for this,
# so I created this one myself.
unrar2dir() {
_archive="${1}" ;
shift ;
_dest_dir="${1}" ;
shift ;
if [ -f "${_archive}" ] ; then
_default_dest_dir="$(basename "${_archive}")" ;
while [ "${_default_dest_dir%[!.]}" != "${_default_dest_dir}" ] ; do
_default_dest_dir="${_default_dest_dir%[!.]}" ;
done ;
_default_dest_dir="${_default_dest_dir%.}" ;
_dir="${_dest_dir:-./"${_default_dest_dir}"}"
unrar-free --version >/dev/null 2>&1 && mkdir -p "${_dir}" &&
(cd "${_dir}" && unrar-free -x /dev/stdin) < "${_archive}" ||
unrar --version >/dev/null 2>&1 ||
unrar x "${_archive}" "${_dir}/" ;
fi ;
}
# I didn't find the original implementation for this,
# so I created this one myself.
unzip2dir() {
_archive="${1}" ;
shift ;
_dest_dir="${1}" ;
shift ;
if [ -f "${_archive}" ] ; then
_default_dest_dir="$(basename "${_archive}")" ;
while [ "${_default_dest_dir%[!.]}" != "${_default_dest_dir}" ] ; do
_default_dest_dir="${_default_dest_dir%[!.]}" ;
done ;
_default_dest_dir="${_default_dest_dir%.}" ;
unzip -d "${_dest_dir:-./"${_default_dest_dir}"}" "${_archive}" ;
fi ;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment