Untitled

mail@pastecode.io avatar
unknown
plain_text
a year ago
714 B
2
Indexable
Never
extract() {
    if [[ ! -f "$1" ]]; then
        echo "$1 is not a file."
        return 1
    fi

    # Detect file type
    file_type=$(file -b "$1")
    case "$file_type" in
        "Zip archive"*)
            # Extract ZIP file with 7zip
            7z x "$1" -o"${1%.*}"
            ;;
        "RAR archive"*)
            # Extract RAR file
            unrar x -o- "$1" "${1%.*}"
            ;;
        "7-zip archive"*)
            # Extract 7z file with 7zip
            7z x "$1" -o"${1%.*}"
            ;;
        *)
            echo "$1 is not a ZIP, RAR or 7z file."
            return 1
            ;;
    esac

    # Remove original file on success
    if [[ $? -eq 0 ]]; then
        rm "$1"
    fi
}