You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何编写可提取各类常见归档文件的Bash函数?

Universal Archive Extractor Bash Function

Hey there! I've got a solid Bash function that handles extracting all the common archive types you mentioned (plus a few more) that I've been using daily. It's flexible, has error checking, and even lets you extract to a specific directory if you want. Let me share it with you:

Here's the full function—you can just copy this into your ~/.bashrc or ~/.bash_profile so it's available every time you open a terminal:

extract() {
    if [ -z "$1" ]; then
        # Display help if no argument given
        echo "Usage: extract <archive-file>"
        echo "Extracts various archive formats:"
        echo "  zip, tar, tar.gz, tar.bz2, tar.xz, rar, 7z, gz, bz2, xz, z"
        return 1
    fi

    local file="$1"
    local extract_dir=""

    # Optional: Extract to a specific directory (pass as second argument)
    if [ -n "$2" ]; then
        extract_dir="$2"
        mkdir -p "$extract_dir" || { echo "Failed to create directory $extract_dir"; return 1; }
    fi

    case "$file" in
        *.zip)
            if command -v unzip &> /dev/null; then
                [ -n "$extract_dir" ] && unzip "$file" -d "$extract_dir" || unzip "$file"
            else
                echo "Error: unzip is not installed"
                return 1
            fi
            ;;
        *.tar)
            [ -n "$extract_dir" ] && tar -xf "$file" -C "$extract_dir" || tar -xf "$file"
            ;;
        *.tar.gz|*.tgz)
            [ -n "$extract_dir" ] && tar -xzf "$file" -C "$extract_dir" || tar -xzf "$file"
            ;;
        *.tar.bz2|*.tbz2)
            [ -n "$extract_dir" ] && tar -xjf "$file" -C "$extract_dir" || tar -xjf "$file"
            ;;
        *.tar.xz|*.txz)
            [ -n "$extract_dir" ] && tar -xJf "$file" -C "$extract_dir" || tar -xJf "$file"
            ;;
        *.rar)
            if command -v unrar &> /dev/null; then
                [ -n "$extract_dir" ] && unrar x "$file" "$extract_dir/" || unrar x "$file"
            else
                echo "Error: unrar is not installed"
                return 1
            fi
            ;;
        *.7z)
            if command -v 7z &> /dev/null; then
                [ -n "$extract_dir" ] && 7z x "$file" -o"$extract_dir" || 7z x "$file"
            else
                echo "Error: p7zip is not installed"
                return 1
            fi
            ;;
        *.gz)
            gunzip "$file"
            ;;
        *.bz2)
            bunzip2 "$file"
            ;;
        *.xz)
            unxz "$file"
            ;;
        *.z)
            uncompress "$file"
            ;;
        *)
            echo "Error: Unsupported file format: $file"
            return 1
            ;;
    esac

    echo "Successfully extracted $file"
}

What makes this function work well?

  • Helpful fallback: If you run extract without any arguments, it shows a clear usage guide and lists all supported formats so you don't have to remember.
  • Custom extraction directory: Pass a second argument to send the extracted files to a specific folder—extract docs.tar.gz my_docs will create my_docs if it doesn't exist and extract there.
  • Dependency checks: For formats that require external tools (like zip, rar, 7z), it first verifies the tool is installed, so you get a clear error message instead of a vague "command not found".
  • Safe error handling: If something goes wrong (invalid file, missing tool, directory creation failure), it exits with an error code and tells you exactly what happened—perfect for using in scripts.

Quick usage examples

  • Extract a zip file to your current directory: extract vacation_photos.zip
  • Unpack a tar.gz into a dedicated project folder: extract project_backup.tar.gz project_files
  • Extract a rar archive: extract important_docs.rar

A few key notes

  • Install required tools:
    • Zip: unzip (use sudo apt install unzip on Debian/Ubuntu, brew install unzip on macOS)
    • Rar: unrar (install via sudo apt install unrar or brew install unrar)
    • 7z: p7zip (go for sudo apt install p7zip-full on Linux, brew install p7zip on macOS)
    • Tar and compression tools (gzip, bzip2, xz) are pre-installed on most Linux and macOS systems.
  • Keep original compressed files: For single-file formats like .gz or .bz2, the function replaces the original with the uncompressed version. If you want to keep the original, add the -k flag to the corresponding command—e.g., change gunzip "$file" to gunzip -k "$file".

内容的提问来源于stack exchange,提问作者arm

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.15 03:31:48