Deleting files after adding to tar archive
BashUnixCompressionTarBash Problem Overview
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.
Bash Solutions
Solution 1 - Bash
With GNU tar, use the option --remove-files
.
Solution 2 - Bash
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 %'
Solution 3 - Bash
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
Solution 4 - Bash
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.