Deleting files after adding to tar archive

2019-01-22 17:03发布

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 the entire tar archive as well as the original files.

3条回答
聊天终结者
2楼-- · 2019-01-22 17:36

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

查看更多
虎瘦雄心在
3楼-- · 2019-01-22 17:56

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 %'
查看更多
男人必须洒脱
4楼-- · 2019-01-22 17:56

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.

查看更多
登录 后发表回答