Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Extract only a single directory from tar

Tags:

python

tar

I am working on a project in python in which I need to extract only a subfolder of tar archive not all the files. I tried to use

tar = tarfile.open(tarfile)
tar.extract("dirname", targetdir)

But this does not work, it does not extract the given subdirectory also no exception is thrown. I am a beginner in python. Also if the above function doesn't work for directories whats the difference between this command and tar.extractfile() ?

like image 699
gaurav Avatar asked Nov 04 '11 11:11

gaurav


People also ask

How do I extract a single directory from a tar file?

Now, if you want a single file or folder from the “tar” file, you need to use the name of the “tar” file and the path to a single file in it. So, we have used the “tar” command with the “-xvf” option, the name of the “tar” file, and the path of a file to be extracted from it as below.

Can you split a tar file?

Split the archive And you can use any option with the tar command as long as you include the '-' option, it sends the tar output to stdout which is then interpreted by the split command.


2 Answers

Building on the second example from the tarfile module documentation, you could extract the contained sub-folder and all of its contents with something like this:

with tarfile.open("sample.tar") as tar:
    subdir_and_files = [
        tarinfo for tarinfo in tar.getmembers()
        if tarinfo.name.startswith("subfolder/")
    ]
    tar.extractall(members=subdir_and_files)

This creates a list of the subfolder and its contents, and then uses the recommended extractall() method to extract just them. Of course, replace "subfolder/" with the actual path (relative to the root of the tar file) of the sub-folder you want to extract.

like image 98
taleinat Avatar answered Oct 24 '22 05:10

taleinat


The other answer will retain the subfolder path, meaning that subfolder/a/b will be extracted to ./subfolder/a/b. To extract a subfolder to the root, so subfolder/a/b would be extracted to ./a/b, you can rewrite the paths with something like this:

def members(tf):
    l = len("subfolder/")
    for member in tf.getmembers():
        if member.path.startswith("subfolder/"):
            member.path = member.path[l:]
            yield member

with tarfile.open("sample.tar") as tar:
    tar.extractall(members=members(tar))
like image 38
Sam Bull Avatar answered Oct 24 '22 04:10

Sam Bull