LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   How to overwrite existing files by using tar command (Remove extra files) (https://www.linuxquestions.org/questions/linux-newbie-8/how-to-overwrite-existing-files-by-using-tar-command-remove-extra-files-4175489175/)

Kalibo 12-25-2013 10:01 PM

How to overwrite existing files by using tar command (Remove extra files)
 
Hello,

I want to overwrite files by using tar command.

But I also want to remove extra files.

for example:
Original files.
a/1,2
TAR files
a/1'

After extraction
a/1' (Because there is no "2" file in tar file.

My question is how to delete file "2" by using tar.

jpollard 12-25-2013 10:39 PM

since you are overwriting files, and want to delete files that aren't in the tar file - the EASIEST way is to just delete everything first.

divyashree 12-25-2013 10:42 PM

Hi Kalibo, Welcome to LQ.

try this.

Code:

tar --delete -f a.tar a/2

Kalibo 12-25-2013 11:26 PM

Is there no other way to delete files that aren't in the tar file after extraction?

There is too risky to delete everything before extraction behavior...

Quote:

Originally Posted by jpollard (Post 5086996)
since you are overwriting files, and want to delete files that aren't in the tar file - the EASIEST way is to just delete everything first.


Kalibo 12-25-2013 11:27 PM

Thank you for reply.

But actually, I want to delete files that aren't in the tar file.
This command is proposed to delete file which is in the tar.

Quote:

Originally Posted by divyashree (Post 5086997)
Hi Kalibo, Welcome to LQ.

try this.

Code:

tar --delete -f a.tar a/2


divyashree 12-25-2013 11:32 PM

Quote:

Originally Posted by Kalibo (Post 5087005)
Thank you for reply.

But actually, I want to delete files that aren't in the tar file.
This command is proposed to delete file which is in the tar.

How can you delete a file that doesn't exist.;) confused ?? You want to delete file from tar or from the source dir ?

Kalibo 12-26-2013 12:27 AM

Sorry for unclear statement.

For example. I have two files under directory A.

A/aaa.txt
A/bbb.txt

1. tar directory A without aaa.txt to backup.tar
=> backup.tar *(without aaa.txt)*

2. Untar backup.tar to directory A
A/aaa.txt -> Not in backup.tar
A/bbb.txt -> Update from backup.tar

I want to remove aaa.txt because there is no aaa.txt file in backup.tar

Quote:

Originally Posted by divyashree (Post 5087008)
How can you delete a file that doesn't exist.;) confused ?? You want to delete file from tar or from the source dir ?


jpollard 12-26-2013 12:37 AM

Quote:

Originally Posted by Kalibo (Post 5087004)
Is there no other way to delete files that aren't in the tar file after extraction?

There is too risky to delete everything before extraction behavior...

Don't like that? rename the directory, then let tar recreate the directory + contents, then delete the renamed directory.

Same result.

Kalibo 12-26-2013 12:41 AM

jpollard,

Thank you for reply.

Because I dont have enough disk size to allocate two directories...

Quote:

Originally Posted by jpollard (Post 5087030)
Don't like that? rename the directory, then let tar recreate the directory + contents, then delete the renamed directory.

Same result.


divyashree 12-26-2013 12:42 AM

Quote:

Originally Posted by Kalibo (Post 5087028)
Sorry for unclear statement.

For example. I have two files under directory A.

A/aaa.txt
A/bbb.txt

1. tar directory A without aaa.txt to backup.tar
=> backup.tar *(without aaa.txt)*

2. Untar backup.tar to directory A
A/aaa.txt -> Not in backup.tar
A/bbb.txt -> Update from backup.tar

I want to remove aaa.txt because there is no aaa.txt file in backup.tar


The option -u is there for tar. Its used to update the tar archive with new file. And then delete/dont include the aaa.txt.
Try this:

PHP Code:

tar -uf A.tar A/bbb.txt 

Then
PHP Code:

tar --delete -f A.tar A/aaa.txt 



All times are GMT -5. The time now is 09:14 AM.