To get the key IDs (8 bytes, 16 hex digits), this is the command which worked for me in GPG 1.4.16, 2.1.18 and 2.2.19:
gpg --list-packets <key.asc | awk '$1=="keyid:"{print$2}'
To get some more information (in addition to the key ID):
gpg --list-packets <key.asc
To get even more information:
gpg --list-packets -vvv --debug 0x2 <key.asc
The command
gpg --dry-run --keyid-format long --import <key.asc
also works in all 3 versions, but in GPG 2.1.18 it prints the key ID of the main key only (and not the subkeys), and in 1.4.16 it prints both the main keys and the subkeys.
Commands in other answers (e.g. gpg --show-keys
, gpg --with-fingerprint
, gpg --import --import-options show-only
) don't work in some of the 3 GPG versions above, thus they are not portable when targeting multiple versions of GPG.