Linux tar remove files

Linux tar remove files

You can remove members from an archive by using the ‘ —delete ’ option. Specify the name of the archive with ‘ —file ’ (‘ -f ’) and then specify the names of the members to be deleted; if you list no member names, nothing will be deleted. The ‘ —verbose ’ option will cause tar to print the names of the members as they are deleted. As with ‘ —extract ’, you must give the exact member names when using ‘ tar —delete ’. ‘ —delete ’ will remove all versions of the named file from the archive. The ‘ —delete ’ operation can run very slowly.

Unlike other operations, ‘ —delete ’ has no short form.

This operation will rewrite the archive. You can only use ‘ —delete ’ on an archive if the archive device allows you to write to any point on the media, such as a disk; because of this, it does not work on magnetic tapes. Do not try to delete an archive member from a magnetic tape; the action will not succeed, and you will be likely to scramble the archive and damage your tape. There is no safe way (except by completely re-writing the archive) to delete files from most kinds of magnetic tape. See section Tapes and Other Archive Media.

To delete all versions of the file ‘blues’ from the archive ‘collection.tar’ in the ‘practice’ directory, make sure you are in that directory, and then,

$ tar --list --file=collection.tar blues folk jazz rock $ tar --delete --file=collection.tar blues $ tar --list --file=collection.tar folk jazz rock

The ‘ —delete ’ option has been reported to work properly when tar acts as a filter from stdin to stdout .

This document was generated on March 24, 2021 using texi2html 5.0.

Читайте также:  Z390 ud v2 linux

Источник

How do I delete a single file from a tar.gz archive

I have a huge tarbell archive with an excessively large or corrupt error_log that causes the archive to hang when attempting to extract it. Is there a way to remove this from the archive before unzipping or extract the archive without extracting that specific file on Mac OS X terminal? I found this post on how to efficiently-remove-files-from-large-tgz however, I tried the —delete flag, but received this error:

tar: Option --delete is not supported 
  1. remove the file from the archive without unzipping it?
  2. extract the archive but exclude the file?

Which version of tar are you using? If its outdated then try to update to the latest version and check the man page if tar supports —delete as an option.

The —delete option is specific to some tar programs (notably the default available under linux). From memory, the default tar under macos does not support that. You’ll need to read the man/help file, or obtain a program that supports such functionality. All else failing, unzip the .tar.gz file, extract everything from the archive, delete the unwanted file, and then rebuild the archive.

Santosh and Peter, thank you so much for your responses. Santosh, not sure which version of tar is running on Mac OS x nor how to update it. Peter, thanks too. This isn’t an option, since extracting the archive fails due to how large the error_log is.

5 Answers 5

As mentioned in the comments it’s not possible to remove the file using tar, but you can exclude the file when extracting:

tar -zxvf file.tar.gz --exclude "file_to_exclude" 

You can repackage it like this:

tar -czvf ./new.tar.gz --exclude='._*' @old.tar.gz 

I used ._* to remove all ._files , but you can use any pattern you like, including a full path, directory, filename, or whatever.

This is the only answer that actually solves my issue of not adding additional files into the tar in the first place. Thanks!

I did that in tree steps. Hopefully will help others in the future.

gzip -d file.tar.gz tar -f file.tar --delete folder1/file1.txt --delete folder2/file2.txt gzip -9 file.tar 

If you have multiple files use this. But the archives them must have all the files you want to delete, or tar will give a error.

for f in *.tar.gz do echo "Processing file $f" gzip -d "$f" tar -f "$" --delete folder1/file1.txt --delete folder2/file2.txt gzip -9 "$" done 

I wanted to remove the jdk directory from the elasticsearch-oss archive with a one liner, and this is what I came up with:

gzip -d elasticsearch-oss-7.10.1-linux-x86_64.tar.gz -c | tar --delete --wildcards */jdk | gzip - > /tmp/tmp.$$.tar.gz && mv /tmp/tmp.$$.tar.gz elasticsearch-oss-7.10.1-linux-x86_64.tar.gz 

I further refined this to include the download:

curl -Ss https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-oss-7.10.1-linux-x86_64.tar.gz | gzip -d - -c | tar --delete --wildcards */jdk | gzip - > elasticsearch-oss-7.10.1-linux-x86_64.tar.gz 

Works a treat on ubuntu 20.04, so gnu tar which does not support the @ sign.

Читайте также:  Linux монтируем сетевую папку

Источник

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.

yes, I have a directory called images. I was doing tar cvjf images.tar.bz2 images/ but I ran out of disk space.

One solution can be to do ftp for entire files to another machine —> archive it —> then move back «archived files» to the same machine

4 Answers 4

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

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?

@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.

I had a task — archive files and then remove into OS installed «tar» without GNU-options.

Method:

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

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

Читайте также:  Tp link установка линукс

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 

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.

note the flag that’s truly necessary here is -r , «append», although -u , «update», also includes it.

I’m not sure if you can add files to bzip2 archives without first extracting. However here is one solution that just came to my mind (giving you the pseudoish algorithm):

1. For each [file] in [all small files] 1.1 compress [file] into [file].bz2 1.2 (optionally verify the process in some way) 1.3 delete [file] 2. For each [bzfile] in [all bzip files from step 1] 2.1 append to tar (tar rvf compressedfiles.tar [bzfile] 2.2 (optionally verify the process in some way) 2.3 delete [bzfile] 

Now you should have a tar file containing all files individually bzip2:ed files. The question is how much overhead bzip2 adds to the individual files. This needs to be tested.

Источник

Оцените статью
Adblock
detector