Deleting files after adding to tar archive

末鹿安然 提交于 2019-12-03 06:29:22

问题


Can GNU tar add many files to an archive, deleting each one as it is added?

This is useful when there is not enough disk space to hold both the entire tar archive and the original files - and therefore it is not possible to simply manually delete the files after creating an archive in the usual way.


回答1:


With GNU tar, use the option --remove-files.




回答2:


I had a task - archive files and then remove into OS installed "tar" without GNU-options.

Method:

Use "xargs"

Suppose, we are have a directory with files.
Need move all files, over the week into tar and remove it.
I do one archive (arc.tar) and added files to it. (You can create new archive every try)

Solution:

find ./ -mtime +7 | xargs -I % sh -c 'tar -rf arc.tar % ; rm -f %'



回答3:


For non GNU tar, you can use "-u" to proccess file per file in a loop

tar -cf archive.tar afile
for myfile in dir/*.ext
do
    tar -uf archive.tar $myfile && rm $myfile || echo "error tar -uf archive.tar $myfile"
done



回答4:


I'm not sure if you can add files to bzip2 archives without first extracting. However here is one solution that just came to my mind (giving you the pseudoish algorithm):

1. For each [file] in [all small files]
    1.1 compress [file] into [file].bz2
    1.2 (optionally verify the process in some way)
    1.3 delete [file]
2. For each [bzfile] in [all bzip files from step 1]
    2.1 append to tar (tar rvf compressedfiles.tar [bzfile]
    2.2 (optionally verify the process in some way)
    2.3 delete [bzfile]

Now you should have a tar file containing all files individually bzip2:ed files. The question is how much overhead bzip2 adds to the individual files. This needs to be tested.



来源:https://stackoverflow.com/questions/10781609/deleting-files-after-adding-to-tar-archive

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!