Deleting files after adding to tar archive

牧云@^-^@ 提交于 2019-12-02 19:59:22
Fred Foo

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

Cristopher Plasma

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 %'

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
Tobbe

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.

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