如何编写可提取各类常见归档文件的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
extractwithout 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_docswill createmy_docsif 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(usesudo apt install unzipon Debian/Ubuntu,brew install unzipon macOS) - Rar:
unrar(install viasudo apt install unrarorbrew install unrar) - 7z:
p7zip(go forsudo apt install p7zip-fullon Linux,brew install p7zipon macOS) - Tar and compression tools (gzip, bzip2, xz) are pre-installed on most Linux and macOS systems.
- Zip:
- Keep original compressed files: For single-file formats like
.gzor.bz2, the function replaces the original with the uncompressed version. If you want to keep the original, add the-kflag to the corresponding command—e.g., changegunzip "$file"togunzip -k "$file".
内容的提问来源于stack exchange,提问作者arm
相关产品推荐
相关产品推荐

