Untar tarball with unique file names

I have a .tar file to extract, which looks like this:

tar tvf bla
100   part1/part1
336   part1/part1
754   part1/part1
638   part1/part1

The folder and file names have the same name, even if the files themselves are unique. When I extract the tar file, "part1" is overwritten several times, and I only have the last "part1" left. How can I extract these files and give them different names? I really don't care what the names are, I just need them to be different, so when I extract them, none of them are overwritten.

One possible way out could be

part1/part1.1
      part1.2
      part1.3
      part1.4
+4
source share
1 answer

I came up with a Python solution that works

import tarfile
tar = tarfile.open([SOMEFILE])
offset = 1
for tarinfo in tar:
   tarinfo.name = "parts1." + str(offset)
   tar.extract(tarinfo, path="parts1")
   offset +=1

It creates

  parts1/parts1.1
        parts1.2
        parts1.3
        parts1.4
0
source

Source: https://habr.com/ru/post/1589432/


All Articles