bash:仅提取 tar.gz 存档的一部分 [英] bash: extract only part of tar.gz archive

查看:31
本文介绍了bash:仅提取 tar.gz 存档的一部分的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个非常.tar.gz文件,由于空间不足,我无法将其全部提取出来.我想提取 一半的内容,处理它们,然后提取剩下的一半.

I have a very large .tar.gz file which I can't extract all together because of lack of space. I would like to extract half of its contents, process them, and then extract the remaining half.

存档包含几个子目录,这些子目录又包含文件.当我提取一个子目录时,我需要用它提取所有它的内容.

The archive contains several subdirectories, which in turn contain files. When I extract a subdirectory, I need all its contents to be extracted with it.

bash 中执行此操作的最佳方法是什么?tar 是否已经允许这样做?

What's the best way of doing this in bash? Does tar already allow this?

推荐答案

好的,所以基于 这个答案,我可以列出所需深度的所有内容.就我而言,tar.gz 文件的结构如下:

OK, so based on this answer, I can list all contents at the desired depth. In my case, the tar.gz file is structured as follows:

archive.tar.gz:
archive/
archive/a/
archive/a/file1
archive/a/file2
archive/a/file3
archive/b/
archive/b/file4
archive/b/file5
archive/c/
archive/c/file6

所以我想遍历子目录 a, b, c ,例如提取其中的前两个:

So I want to loop over subdirectories a, b, c and, for instance extract the first two of them:

parent_folder='archive/'
max_num=2
counter=0
mkdir $parent_folder
for subdir in `tar --exclude="*/*/*" -tf archive.tar.gz`; do
    if [ "$subdir" = "$parent_folder" ];
    then
        echo 'not this one'
        continue        
    fi
    if [ "$counter" -lt "$max_num" ];
    then
        tar zxvf archive.tar.gz $subdir -C ./${parentfolder}${subdir}
        counter=$((counter + 1))
    fi
done

接下来,对于剩余的文件:

Next, for the remaining files:

max_num=2
counter=0
mkdir $parent_folder
for subdir in `tar --exclude="*/*/*" -tf files.tar.gz`; do
    if [ "$subdir" = "$parent_folder" ];
    then
        echo 'not this one'
        continue        
    fi
    if [ "$counter" -ge "$max_num" ];
    then
        tar zxvf files.tar.gz $subdir -C ./${parent_folder}$subdir
    fi
    counter=$((counter + 1))
done

这篇关于bash:仅提取 tar.gz 存档的一部分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆