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
    Replacing: you mean in the files names or in the files's contents? Commented Jan 12, 2022 at 10:57
  • Duplicating the directory before tar is not an option I guess, right?
    – pLumo
    Commented Jan 12, 2022 at 10:57
  • It has to be a one-liner? Commented Jan 12, 2022 at 11:40
  • changed my question to reflect your additions. Edit your question to include your edits simply in the text; no need to explicitly mark them as edits in this context. Commented Jan 12, 2022 at 13:26
  • Do all the files in the directory need to be changed, or they are some special kind of files? Commented Jan 12, 2022 at 16:02

1 Answer 1


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.

You must log in to answer this question.

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