ex.info
A versatile shell script designed to simplify the extraction of various archive formats by automatically detecting the file type and applying the appropriate extraction method.
ChatGPT has contributed to this document. Therefore, it’s advisable to treat the information here with caution and verify it if necessary.
Let’s break down what this script does in detail. The script is named ex.sh
,
authored by GJS (homelab-alpha), and its purpose is to provide a convenient way
to extract various archive formats. It simplifies the extraction process by
automatically detecting the file format and applying the appropriate extraction
method.
Here’s a detailed explanation:
Script Metadata
- Filename:
ex.sh
- Author: GJS (homelab-alpha)
- Date: May 26, 2024
- Version: 1.0.1
- Description: This script provides a convenient way to extract various archive formats including tar, zip, gzip, bzip2, rar, etc. It simplifies the extraction process by automatically detecting the file format and applying the appropriate extraction method.
- RAW Script: ex.sh
Function Declaration
# Function: ex
function ex() {
The script defines a function called ex
. This function is responsible for
handling the extraction of different archive formats.
File Existence Check
# Check if the input file exists
if [ -f "$1" ]; then
The function starts by checking if the input file (passed as the first argument
$1
) exists using the -f
flag.
Case Statement for File Type Detection
# Determine the file type and perform extraction accordingly
case "$1" in
The script uses a case
statement to determine the file type based on the file
extension of the input file.
Handling Different File Types
Each case within the case
statement handles a specific file extension:
.tar.bz2
: Extracts usingtar -jxvf
*.tar.bz2) tar -jxvf "$1" ;;
.tar.gz
: Extracts usingtar -zxvf
*.tar.gz) tar -zxvf "$1" ;;
.bz2
: Extracts usingbunzip2
*.bz2) bunzip2 "$1" ;;
.dmg
: Mounts the disk image usinghdiutil
*.dmg) hdiutil mount "$1" ;;
.gz
: Extracts usinggunzip
*.gz) gunzip "$1" ;;
.tar
: Extracts usingtar -xvf
*.tar) tar -xvf "$1" ;;
.tbz2
: Extracts usingtar -jxvf
*.tbz2) tar -jxvf "$1" ;;
.tgz
: Extracts usingtar -zxvf
*.tgz) tar -zxvf "$1" ;;
.zip
or.ZIP
: Extracts usingunzip
*.zip | *.ZIP) unzip "$1" ;;
.pax
: Extracts usingpax
*.pax) pax -r <"$1" ;;
.pax.Z
: Uncompresses and extracts usinguncompress
piped topax
*.pax.Z) uncompress "$1" --stdout | pax -r ;;
.rar
: Extracts usingunrar
*.rar) unrar x "$1" ;;
.Z
: Extracts usinguncompress
*.Z) uncompress "$1" ;;
Unsupported File Format
*) echo "$1 cannot be extracted/mounted via ex()" ;; # Unsupported file format
If the file extension does not match any of the supported formats, the script prints a message indicating that the file cannot be extracted.
Non-Existent File Handling
else
echo "$1 is not a valid file" # Print error message for non-existent files
fi
If the input file does not exist, the script prints an error message indicating that the file is not valid.
Conclusion
This script effectively handles the extraction of various archive formats, making it a handy tool for simplifying the process of dealing with compressed files.
Last updated 22 Sep 2024, 12:15 CEST .