MacOS – How to combine unzip and cat to work as one in the terminal

macmacosterminal

I work for a webhost as a front level tech and often we need to install a SSL for a customer. I'm on a Mac now but am familiar with Linux as well.

What I'm looking to do would be to take the zipped file, download it to my mac and then run the command to both unzip it and cat at one pop. Thus eliminating the tedious process of unzip file.zip and then copy each file, one at a time to run cat.

I'm lame when it comes to writing a script and have played around with multiple commands, none that worked out.

Best Answer

So I've done some testing, and it seems that gzcat (and event zcat) on OS X only work for gzipped files, and not files using standard zip compression. That being said, I believe this is what you are looking for:

"sub.domain.tld.ssl.zip" contains 2 files:

  • "sub.domain.tld.crt" (SSL certificate)
  • "sub.domain.tld.key" (RSA private key)

In order to print all files to STDOUT, you would use unzip -p

To "cat" the certificate, you could use the following command:

unzip -p sub.domain.tld.ssl.zip | sed -n '/CERT/,/CERT/p'

To "cat" the private key, you could use the following command:

unzip -p sub.domain.tld.ssl.zip | sed -n '/KEY/,/KEY/p'

Afterwards, of which you could do what you wish, such as create a little script or function:

#!/bin/bash
unzip -p "$1" | sed -n '/CERT/,/CERT/p' > "/etc/ssl/Certs/${2}.crt"
unzip -p "$1" | sed -n '/KEY/,/KEY/p' > "/etc/ssl/Private/${2}.key"

The above script would take 2 arguments (which should ideally be enclosed in quotes):

  1. File name of zip file
  2. Naming convention of cert/key files

If the name of the script was sslinstazip.sh, you would run it like so:

./sslinstazip.sh "sub.domain.tld.ssl.zip" "sub.domain.tld"

There are obviously many ways you can modify this to fit your own personal needs, but in this case, unzip is actually your friend.