Modify file names in tar file without extracting files to temporary location

commandcommand linetar

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.

Best Answer

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