74

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.

Ivy
  • 3,393
  • 11
  • 33
  • 46
  • Are ALL of the files which exist in the directory must be compressed? – Eugene S May 28 '12 at 08:48
  • Doesn't `man tar` answer your question? – lanzz May 28 '12 at 08:49
  • yes, I have a directory called images. I was doing tar cvjf images.tar.bz2 images/ but I ran out of disk space. – Ivy May 28 '12 at 08:49
  • One solution can be to do ftp for entire files to another machine --> archive it --> then move back "archived files" to the same machine – Pradeep May 28 '12 at 09:57
  • I'd rather not do that, as there are a lot of files, and I may need to do it again with even more – Ivy May 28 '12 at 13:05
  • @lanzz No, as the bsd tar *doesn't* have the `--remove-files` option as shown in the gnu tar man page. – bfcoder Aug 27 '18 at 14:32
  • @bfcoder No, it would still answer the question. If you're on bsd, `man tar` would show you the man page for bsd tar, not for gnu tar. – lanzz Aug 27 '18 at 14:41

4 Answers4

103

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

xinthose
  • 3,213
  • 3
  • 40
  • 59
Fred Foo
  • 355,277
  • 75
  • 744
  • 836
6

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 %'
Ivy
  • 3,393
  • 11
  • 33
  • 46
Mikro Koder
  • 1,056
  • 10
  • 13
  • 5
    1. I would use `&&` instead of `;` between `tar` and `rm` commands, so only remove files if their addition to the archive was successful. 2. Consider using `-exec` option if your `find` has it. https://stackoverflow.com/a/6043896/711006 – Melebius Nov 27 '17 at 07:51
4

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
E D
  • 41
  • 2
  • Welcome to Stack Overflow! While this code may solve the question, [including an explanation](https://meta.stackexchange.com/questions/114762/explaining-entirely-code-based-answers) of how and why this solves the problem would really help to improve the quality of your post, and probably result in more up-votes. Remember that you are answering the question for readers in the future, not just the person asking now. Please [edit] your answer to add explanation, and give an indication of what limitations and assumptions apply. – double-beep Feb 21 '19 at 14:57
  • note the flag that's truly necessary here is `-r`, "append", although `-u`, "update", also includes it. – SpinUp __ A Davis Jun 23 '23 at 17:28
0

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.

alex
  • 479,566
  • 201
  • 878
  • 984
Tobbe
  • 149
  • 8