0

Consider an arbitrary folder $DIR with files and folders in it. I want to TAR this folder to $DIR.tar, but before, as a preliminary step, in all files I want to change string occurrences of FOO to BAR in the file contents.

Just to be specific: the change shouldn't affect the files in $DIR, it should just manifest in $DIR.tar

Is it possible to do this in GNU/bash?

I can do it by creating a copy of $DIR, sed it and tar it separately. I was hoping that there is an in-memory solution.

1 Answers1

2

The easiest solution of course is to make a copy of your directory, then do your modifications, then do your taring. And that's basically free in terms of disk space (aside from the changed blocks) and time effort if the file system you're doing this on is Btrfs, CIFS, NFS 4.2, OCFS2, overlayfs, or XFS, as these support reflinks, i.e., copying a file doesn't actually copy the file's content. Only when the contents are changed, a copy of the original data extent is made. Use cp --reflink=always.

As GNU tar has no way to take input from anything but files, and if you specify a file, the path of that file will be significant, as it reflects the path in the archive, I'm afraid you'll not be able to do it any other way using tar.

I was hoping that there is an in-memory solution.

The by far easiest way is picking a scripting language that is reasonable usable to you, and building your archive yourself instead of depending on the tar program. For example, Python comes with the tarfile module; it's easy to use, even for Python beginners. There's a lot of answers that contain that, some might be helpful to your solution. Haven't checked them, though.