2

is there a way to use tar to pack a directory and check the source data for changes after taring?

Well I want to pack a folder. But it can happen that the content of the folder is changed during the packing process. I need a way for tar to check if the source files have changed after the archive has been created. If so, the packed files should be overwritten with the new source files

NETFLOX
  • 125

2 Answers2

2

you could rsync the directory to a temporary place, then you keep checking with rsync until there are no changes, and finally you make your tar file.

2

If you want to update just the contents of the existing files and also newer files (but not deleting the files from the archive which are no longer in the current directory ,because that will give you the error No such file or directory trying to update the archive ) , you can use the diff feature as @Niels Tolstrup mentioned and then use --update or simply -u to update the archive contents :

tar -uf tarfile.tar `tar --diff --file tarfile.tar | awk ' { print $1 } ' | cut -d: -f1`

Note : Tar update will make many duplicate entries in the tar file which you can see them via tar --list --file tarfile.tar but that's not a problem when you extract the files.

Parsa Mousavi
  • 3,305
  • 16
  • 37
  • thats nice, is there a way or a hack to do that with compressed archives? – NETFLOX Jun 12 '20 at 10:02
  • @Rudi For zip files it's quite straightforward .For xz and gzip files you have to first gunzip or unxz them ( which might be not suitable for you because you mentioned you have not enough disk space available) , then update the contents using the above method and finally recompress them. – Parsa Mousavi Jun 12 '20 at 10:48
  • zip sounds an alternative, but if i try to append file to an exist zip, zip repack the whole archive. That mean, zip need disk space for two archives in zip process – NETFLOX Jun 12 '20 at 11:58
  • @Rudi So what about gzip or xz ? Do they have the same problem ? – Parsa Mousavi Jun 12 '20 at 11:59
  • i cant append files to an compressed tgz :( but i need it compressed. xz I have not yet examined, i will do it – NETFLOX Jun 12 '20 at 12:03
  • nope, no way. xz is slow as underground. I have tried 7z with the update option, but the same there. The archive will repacked completely..

    I need an compressed archive where i can update only diffs to the originals.. this should be possible, maybe this format should use an index, but it must be possible :(

    – NETFLOX Jun 12 '20 at 13:10
  • @Rudi Tar archives are actually like a filesystem . You can easily add or remove a file or folder within them. But compressed archives are like the jpeg format.Contents are compressed as a whole. You cannot edit a jpeg file without decoding it first.So I don't think it's possible to do the same with compressed archives. If you found a solution for it , I'm glad to hear. – Parsa Mousavi Jun 12 '20 at 14:23