index rss mastodon twitter github linkedin email
Álvaro Ramírez
sponsor

Álvaro Ramírez

16 July 2022 A lifehack for your shell

unzip_x2.gif

I'm a fan of the unzip command line utility that ships with macOS. I give it a .zip file and it unzips it for me. No flags or arguments to remember (for my typical usages anyway). Most importantly, I've fully internalized the unzip command into muscle memory, probably because of its perfect mnemonic.

But then there's .tar, .tar.gz, .tar.xz, .rar, and a whole world of compression archives, often requiring different tools, flags, etc. and I need to remember those too.

Can't remember where I got this "life hack" from, but it suggests something along the lines of…

Once you find a lost item at home, place it in the first spot you looked.

Great, I'll find things quickly. Win.

Now, I still remember a couple of unarchiving commands from memory (looking at you tar xvzf), but I've noticed the first word that pops into mind when extracting is always unzip.

There's the great atool wrapper out there to extract all kinds of archives (would love to hear of others), but unlucky for me, its name never comes to mind as quickly as unzip does.

With "life hack" in mind, let's just create an unzip eshell alias to atool. Next time I need to unarchive anything, the first word that comes to mind (unzip!) will quickly get me on my way…

alias unzip 'atool --extract --explain $1'

Or if you prefer to add to your Emacs config:

(eshell/alias "unzip" "atool --extract --explain $1")

While I'm fan of Emacs eshell, it's not everyone's cup of tea. Lucky for us all, aliases are a popular feature across shells. Happy unzipping!

Bonus

Since I'm a keen on using "unzip" mnemonic everywhere in Emacs (not just my shell), I now have a DWIM shell-command for it:

(defun dwim-shell-command-unzip ()
  "Unzip all marked archives (of any kind) using `atool'."
  (interactive)
  (dwim-shell-command-on-marked-files
   "Unzip" "atool --extract --explain '<<f>>'"
   :utils "atool"))

unzip-dired_x1.5.gif

UPDATE:

Lobste.rs has great comments. Thanks all:

Aliases missing on remote machines

Concerns about aliases not available on remote machines. Valid. Certainly brings challenges if you can't modify the environment on the remote machine. The severity would depend on how frequently you have to do this. Fortunately for me, it's infrequent.

Additionally, if accessing remote machine via eshell, this is a non-issue. You get to transparently bring most of your environment with you anyway.

Unzip keyword is overloaded

The alias is overloading the unzip command. I know. It's a little naughty. Going with it for now. I used to use "extract" (also in comments), which I still like but somehow "unzip" still wins my memory race. There's also "x" (nice option), which seems to originate from prezto. I could consider unzipp, unzip1, or some other variation.

Not sure how I missed this, but there's also an existing alias for atool: aunpack. Could be a great alternative.

Pause before extracting archives

Valid point. In my case, the pause typically happens before I invoke the alias.

Littering

If the archive didn't have a root dir, it can litter your current directory. Indeed a pain to clean up. For this, we can atool's --subdir param to always create subdirectory when extracting.

Alias to retrain

Neat trick: alias unzip = “echo ‘use atool’” to help retrain yourself. Reminds me of Emacs guru-mode.

atool alternatives

Nice to see other options suggested dtrx (comment), archiver (comment), unar (comment), bsdtar from libarchive (comment), unp, patool, and the tangentially related zgrep (comment).