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
Comments
-
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 about 12 yearsAre 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 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 almost 11 yearsHow about bsdtar? Anyone?
-
Viacheslav Dobromyslov over 9 yearsWorks great. Helps to make backups by pg_dump -Fd
-
Melebius over 6 years1. I would use
&&
instead of;
betweentar
andrm
commands, so only remove files if their addition to the archive was successful. 2. Consider using-exec
option if yourfind
has it. stackoverflow.com/a/6043896/711006 -
double-beep over 5 yearsWelcome 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.