Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Deleting files after adding to tar archive

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.

like image 651
Ivy Avatar asked May 28 '12 08:05

Ivy


People also ask

Does tar overwrite existing files?

Changing How tar Writes Files. Normally, tar writes extracted files into the file system without regard to the files already on the system--files with the same name as archive members are overwritten.

How do I delete a single file from tar?

1) How to remove a single file from tar file To test the files in a tar file you can use “-t” switch with tar command. We can use the “–delete” switch with tar command to remove files from already created tar file.

How do you delete files from archive?

In the Archive Delete window, expand the directory tree by clicking the plus sign (+) or folder icon next to the object you want to expand. Objects on the tree are grouped by archive package description. Select the archived objects that you want to delete. Click Delete.

How do I add files to an existing tar file?

The simplest way to add a file to an already existing archive is the ' --append ' (' -r ') operation, which writes specified files into the archive whether or not they are already among the archived files. When you use ' --append ', you must specify file name arguments, as there is no default.


2 Answers

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

like image 134
Fred Foo Avatar answered Oct 11 '22 11:10

Fred Foo


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 %' 
like image 21
Mikro Koder Avatar answered Oct 11 '22 10:10

Mikro Koder