function extract --description "Extract various archive formats" if test (count $argv) -lt 1 echo "Usage: extract [output_directory]" return 1 end if not command -q file echo "extract: 'file' command is required. Install it via your package manager." return 1 end set archives $argv set output_dir "." set use_custom_dir 0 if test (count $argv) -eq 2 set maybe_dir $argv[2] if test -d "$maybe_dir"; or not test -e "$maybe_dir" set output_dir "$maybe_dir" mkdir -p "$output_dir" set archives $argv[1] set use_custom_dir 1 end end set multi 0 if test (count $archives) -gt 1 set multi 1 end for archive in $archives if not test -f "$archive" echo "extract: '$archive' is not a valid file" continue end set base (string replace -r '\.(tar\.(gz|bz2|xz|zst)|tar|tgz|tbz2|txz|tzst|gz|bz2|xz|zst|zip|7z|rar)$' '' (basename "$archive")) if test $multi -eq 1 set target_dir "$base" else if test $use_custom_dir -eq 1 set target_dir "$output_dir" else set target_dir "$base" end set mime (file --mime-type -b "$archive") switch $mime case 'application/x-tar' if not command -q tar echo "extract: 'tar' is not installed. Install it via your package manager." continue end mkdir -p "$target_dir" tar -xvf "$archive" -C "$target_dir" echo "extract: extracted '$archive' to '$target_dir'" case 'application/gzip' if string match -q '*.tar.gz' -- "$archive"; or string match -q '*.tgz' -- "$archive" if not command -q tar echo "extract: 'tar' is not installed. Install it via your package manager." continue end mkdir -p "$target_dir" tar -xzf "$archive" -C "$target_dir" echo "extract: extracted '$archive' to '$target_dir'" else if not command -q gzip echo "extract: 'gzip' is not installed. Install it via your package manager." continue end set outname (string replace -r '\.gz$' '' (basename "$archive")) gzip -dkc "$archive" > "$output_dir/$outname" echo "extract: decompressed '$archive' to '$output_dir/$outname'" end case 'application/x-bzip2' if string match -q '*.tar.bz2' -- "$archive"; or string match -q '*.tbz2' -- "$archive" if not command -q tar echo "extract: 'tar' is not installed. Install it via your package manager." continue end mkdir -p "$target_dir" tar -xjf "$archive" -C "$target_dir" echo "extract: extracted '$archive' to '$target_dir'" else if not command -q bzip2 echo "extract: 'bzip2' is not installed. Install it via your package manager." continue end set outname (string replace -r '\.bz2$' '' (basename "$archive")) bzip2 -dkc "$archive" > "$output_dir/$outname" echo "extract: decompressed '$archive' to '$output_dir/$outname'" end case 'application/x-xz' if string match -q '*.tar.xz' -- "$archive"; or string match -q '*.txz' -- "$archive" if not command -q tar echo "extract: 'tar' is not installed. Install it via your package manager." continue end mkdir -p "$target_dir" tar -xJf "$archive" -C "$target_dir" echo "extract: extracted '$archive' to '$target_dir'" else if not command -q xz echo "extract: 'xz' is not installed. Install it via your package manager." continue end set outname (string replace -r '\.xz$' '' (basename "$archive")) xz -dkc "$archive" > "$output_dir/$outname" echo "extract: decompressed '$archive' to '$output_dir/$outname'" end case 'application/zstd' if string match -q '*.tar.zst' -- "$archive"; or string match -q '*.tzst' -- "$archive" if not command -q tar echo "extract: 'tar' is not installed. Install it via your package manager." continue end mkdir -p "$target_dir" tar --zstd -xvf "$archive" -C "$target_dir" echo "extract: extracted '$archive' to '$target_dir'" else if not command -q zstd echo "extract: 'zstd' is not installed. Install it via your package manager." continue end set outname (string replace -r '\.zst$' '' (basename "$archive")) zstd -dkc "$archive" > "$output_dir/$outname" echo "extract: decompressed '$archive' to '$output_dir/$outname'" end case 'application/zip' if not command -q unzip echo "extract: 'unzip' is not installed. Install it via your package manager (e.g., 'unzip' on most distros)." continue end mkdir -p "$target_dir" unzip "$archive" -d "$target_dir" echo "extract: extracted '$archive' to '$target_dir'" case 'application/x-7z-compressed' if command -q 7z mkdir -p "$target_dir" 7z x "$archive" -o"$target_dir" echo "extract: extracted '$archive' to '$target_dir'" else if command -q 7zz mkdir -p "$target_dir" 7zz x "$archive" -o"$target_dir" echo "extract: extracted '$archive' to '$target_dir'" else echo "extract: '7z' or '7zz' is not installed. Install p7zip via your package manager." continue end case 'application/x-rar' 'application/vnd.rar' if command -q unrar mkdir -p "$target_dir" unrar x -o+ "$archive" "$target_dir/" echo "extract: extracted '$archive' to '$target_dir'" else if command -q rar mkdir -p "$target_dir" rar x -o+ "$archive" "$target_dir/" echo "extract: extracted '$archive' to '$target_dir'" else echo "extract: 'unrar' or 'rar' is not installed. Install unrar via your package manager." continue end case '*' echo "extract: unknown file type for '$archive' ($mime)" continue end end end