What is the easiest way to list all the user:group found in a tarball?

Interesting question. From a quick look through the man page (searching for "user" and when that didn't turn up results, searching for "owner") the following should do it:

tar xf thetarball.tgz --to-command='sh -c "echo $TAR_UNAME $TAR_GNAME"' | sort | uniq -c

Obviously, change the script according to your needs. You might want $TAR_UID and $TAR_GID instead of the names for some use cases.

I recommend also that you read up on the --owner-map and --group-map options for tar; they sound like they could greatly benefit your use case and would be a lot simpler than creating all the users and groups ahead of time.


Quickly assembled:

groups:

tar tvf thetarball.tgz | awk '{print $2}' | cut -d/ -f2 | sort -u

users:

tar tvf thetarball.tgz | awk '{print $2}' | cut -d/ -f1 | sort -u

user/groups pairs:

tar tvf thetarball.tgz | awk '{print $2}' | sort -u

Tags:

Tar

Users

Group