Deleting files after adding to tar archive Deleting files after adding to tar archive bash bash

Deleting files after adding to tar archive


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


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 %'


For non GNU tar, you can use "-u" to proccess file per file in a loop

tar -cf archive.tar afilefor myfile in dir/*.extdo    tar -uf archive.tar $myfile && rm $myfile || echo "error tar -uf archive.tar $myfile"done