8

I have a tar file that contains some files in a folder named old_name. Now I'd like to create a new tar file where that folder has been renamed to new_name without extracting to file to disk as that would be significantly slower for large archives (More than double the disk reads and writes).
I know how to do that: tar -xf old.tar; tar -cf new.tar --transform 's/old_name/new_name/' old_name

I've tried a few things but none seemed to work:

  • tar -cOf old.tar | tar -xf new.tar --transform 's/old_name/new_name/'
  • cat old.tar | tar --delete --transform 's/old_name/new_name/' > new.tar
  • cat old.tar | tar -u --transform 's/old_name/new_name/' > new.tar

But nothing seems to work.

Closed I've found are these:

But those are about removing files in the tarball, not changing their paths.

BrainStone
  • 3,654

2 Answers2

4

tar can create or extract tar's but it can't operate on streams in this way.

You'll need something like tar-stream - it even has an example that does what you are asking:

https://github.com/mafintosh/tar-stream#modifying-existing-tarballs

laktak
  • 5,946
2

Did you try archivemount to mount your archive on a mountpoint, rename your directory, then unmount the archive?

$ archivemount archive.tar /mnt/archive

$ ls /mnt/archive /mnt/archive/archive/file1 /mnt/archive/archive/file2

$ mv /mnt/archive/archive/file2 /mnt/archive/archive/file3

$ ls /mnt/archive /mnt/archive/archive/file1 /mnt/archive/archive/file3

$ umount /mnt/archive

$ tar -tf archive.tar archive/file1 archive/file3

It will move the original archive to archive.tar.orig

$ tar -tf archive.tar.orig archive/file1 archive/file2

I found the tool in this related answer: How to add/update a file to an existing tar.gz archive?. It also mentions a similar tool tarfs.

duthils
  • 426
  • 3
  • 4