bash 添加到 tar 存档后删除文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10781609/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Deleting files after adding to tar archive
提问by Rory
Can GNU tar add many files to an archive, deleting each one as it is added?
GNU tar 可以将许多文件添加到存档中,并在添加时删除每个文件吗?
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.
当没有足够的磁盘空间来保存整个 tar 存档和原始文件时,这很有用 - 因此不可能在以通常的方式创建存档后简单地手动删除文件。
回答by Cristopher Plasma
I had a task - archive files and then remove into OS installed "tar" without GNU-options.
我有一个任务 - 存档文件,然后在没有 GNU 选项的情况下删除到操作系统安装的“tar”。
Method:
方法:
Use "xargs"
使用“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)
假设,我们有一个包含文件的目录。
需要在一周内将所有文件移动到 tar 中并将其删除。
我做了一个存档(arc.tar)并向其中添加了文件。(您可以每次尝试创建新存档)
Solution:
解决方案:
find ./ -mtime +7 | xargs -I % sh -c 'tar -rf arc.tar % ; rm -f %'
回答by E D
For non GNU tar, you can use "-u" to proccess file per file in a loop
对于非 GNU tar,您可以使用“-u”在循环中按文件处理文件
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
回答by 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):
我不确定您是否可以在不先解压的情况下将文件添加到 bzip2 档案中。然而,这是我刚刚想到的一种解决方案(给你伪算法):
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.
现在你应该有一个包含所有文件的 tar 文件,分别是 bzip2:ed 文件。问题是 bzip2 给单个文件增加了多少开销。这是需要测试的。