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.

4
  • @Quasímodo why does that matter? But in my particular case it’s around 1GB
    – BrainStone
    Jul 1, 2020 at 11:08
  • You could untar it in shared memory (in my system, /dev/shm), so that it is in RAM, not on disk.
    – Quasímodo
    Jul 1, 2020 at 11:19
  • I don’t have that available.
    – BrainStone
    Jul 1, 2020 at 11:20
  • when creating/extracting there is --transform s/foo/bar/, it won't modify your archive file a you wanted. i mention for t anyone who may find it a suitable work around to only apply this rename/fix at extract time Jul 7, 2020 at 18:21

2 Answers 2

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

1
  • Thank you for showing this project, but NPM is not available on my target systems.
    – BrainStone
    Jul 1, 2020 at 12:19
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.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .