Search code examples
linuxbashmaxmindgunzip

Script to extract only the MaxMind GeoLite2 Country database from gzip containing multiple files


Recently MaxMind changed their download policy, and the old simple format is no longer available. The new file format looks like this: GeoLite2-Country_20191231.tar.gz, and inside we have a folder with the same name containing two additional files.

Although there is an option to delete the date parameter from the link, it seems that the downloaded file will still contain the date.

Now, the problem is to extract that GeoLite2-Country.mmdb from the gzip file having that variable name programmatically.

The unzip part existing in my old script was this:

gunzip -c "$1"GeoLite2-Country.mmdb.gz > "$1"GeoLite2-Country.mmdb

The question is how to modify the above part for the new situation. Or, maybe someone knows another way to solve the same problem. Thanks in advance.

The folder structure:

-+ Geolite2-Country_YYYYMMDD.tar.gz:
 |-+ Geolite2-Country_YYYYMMDD
   |- licence.txt
   |- copyright.txt
   |- Geolite2-Country.mmdb

What I need is Geolite2-Country.mmdb in the current folder of gzip file.


Solution

  • tar -tf /GeoLite2-City.tar.gz | grep mmdb | xargs tar -xf  /GeoLite2-City.tar.gz --strip-components 1 -C /
    

    Just fix source and destination paths