Deleting files after adding to tar archive

后端 未结 4 560
夕颜
夕颜 2021-02-02 05:40

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 th

相关标签:
4条回答
  • 2021-02-02 06:08

    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.

    0 讨论(0)
  • 2021-02-02 06:12

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

    0 讨论(0)
  • 2021-02-02 06:16

    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
    
    0 讨论(0)
  • 2021-02-02 06:19

    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 %'
    
    0 讨论(0)
提交回复
热议问题