Improvement to the `untar` alias, to also extract zips, by turning it into a script

Garuda has an untar alias which is basically tar -xvf.

It has come to my attention that another type of archive, the zip exists.

Anyway, it's pretty easy to let untar also open zips, if you make it a little script. Here's a bash script, though if you want me to make it a fish alias I can do that too. It just feels like people could find it useful, as I wanted it.

It fixes an annoying part of running unzip for me too, which is that some zips dump a bunch of crap into the working directory (it doesn't do analysis of malicious zips that try to store stuff above the working directory, but I'm not being paid to write this either :P)

#!/bin/bash

filename="$1"
extension="${filename##*.}"
name="${filename%.*}"

if [[ "$extension" = "zip" ]] ; then
        if [[ `zipinfo -1 "$1" | sed 's/\/.*//' | sort | uniq | wc -l` = '1' ]] ; then
                unzip "$1"
        else
                mkdir "$name"
                unzip "$1" -d "$name"
        fi
        exit 0
fi
tar -xvf "$1"

You might argue that I should just install 7zip if I want this all in one command. I would reply that there's no reason to have less functionality in your tools when it's easy to add it.

The only actual problem is that nothing about the name untar would make you suspect this functionality exists. But idk how to solve that.

4 Likes

Good stuff man! I mostly deal with zip files as opposed to tar files so a fish alias would be amazing for this script so I can just use one command for tar or zip. I think currently it's 7z e or something along those lines for zip files which is okay anyway. Just sometimes I forget with my bad memory the e or hyphen or package name.

Intresting point there.

Although I'm using peazip since a decade now anyways. But I got your point.

Since you are using bash, it is relatively easy to convert this script to handle multiple files given in cmdline parameters, just by using arrays and a for...loop.
Starting like this:

filename="[email protected]"
extension="${filename[@]##*.}"
name="${filename[@]%.*}"

Just from memory, so not tested :wink: Filenames including spaces always break my code and nerves. :face_with_head_bandage:

3 Likes

Unfortunately, there are hundreds of archive types, and as you've found they don't behave in the same way.

Generally speaking, it's better to have a single application that does a single thing rather than something that tries to do everything - hence tar to operate on tar files, zip to operate on Zip files, etc.

This definitely is an annoying default, but it's also how the Zip file is structured - everything at the top level will be extracted into the current directory. Tar files will do the same thing if they contain files rather than a directory.

6 Likes

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.