List All User:Group in Tarball – Easiest Method

grouptarusers

I'm installing some of my data from my old server to my new server.

Since I had my old server for ages, I have a huge amount of legacy data with, most certainly, legacy user and group names.

When extracting, tar does its best to match the user and group info by name and uses the identifiers as a fallback or the current user as a last resort.

What I'd like to do is make sure that all the users and groups exist before I do the extraction. That way all the files get the correct ids.

To do that, the best way I can think of is to list all the user and group names found in the tar file. I know I can use the tar tvf backup.tar command to list all the files, but then I'd have to come up with a way to extract the right two names.

I'm wondering whether there would be a simpler way than using the tv option. Some tool or command line options that only extracts the user name and group name, the I can use sort -u to reduce the list to unique entries.

Anyone knows of such a feature?

Best Answer

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.