Deleting files after adding to tar archive

64,785

Solution 1

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

Solution 2

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

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
Share:
64,785
Ivy
Author by

Ivy

This space intentionally left blank.

Updated on April 18, 2021

Comments

  • Ivy
    Ivy about 3 years

    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.

  • Eugene S
    Eugene S about 12 years
    Are you sure that by using this flag will remove each file after adding it to the archive rather than deleting all the files in the end of the process?
  • Fred Foo
    Fred Foo about 12 years
    @EugeneS: I checked the GNU tar source code and there is some (configurable) delay between archiving and removal, but tar will not wait for the entire tarball to be created.
  • dpb
    dpb almost 11 years
    How about bsdtar? Anyone?
  • Viacheslav Dobromyslov
    Viacheslav Dobromyslov over 9 years
    Works great. Helps to make backups by pg_dump -Fd
  • Melebius
    Melebius over 6 years
    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. stackoverflow.com/a/6043896/711006
  • double-beep
    double-beep over 5 years
    Welcome to Stack Overflow! While this code may solve the question, including an explanation 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.