Find the answer to your Linux question:
Results 1 to 2 of 2
Hello, thanks for Your visit, i want to ask for advice with linux Tar. I know i can list tar contents by command: tar tf archive.tar If its tar.gz, i ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Linux User postcd's Avatar
    Join Date
    Apr 2011
    Posts
    321

    List tar contents only first level, then subdirectory & extract


    Hello,

    thanks for Your visit, i want to ask for advice with linux Tar.

    I know i can list tar contents by command: tar tf archive.tar
    If its tar.gz, i can: tar tzf archive.tar.gz

    But my tar.gz is 40Gb large and its whole linux filesystem.

    please kindly advice me on how i can list only arcive root directory. Then i see folder name that interest me, then how i can list only that folder contents (non recursivelly) and this way browse deep into archive. Then how i can extract example folder or file /archive.tar.gz/folder/subfolder/fileorfolder

    Please can You write commands?

    Thank you alot
    "Avoid the Gates of Hell. Use Linux affordable VPS."

  2. #2
    Linux Enthusiast
    Join Date
    Jan 2005
    Location
    Saint Paul, MN
    Posts
    648
    Easiest way is use the Unix/Linux tools together to give you what you desire.

    For example, I have the following subtree:
    Code:
                                  /   /file_01
                                 /   /file_02
                                /one/file_03
                               /   /file_04
                              /
                             /   /file_01
                            /   /file_02
                           /two/file_03
                          /   /file_04
                         /
                        /     /file_01
                       /     /file_02
                      /three/file_03
                     /     /file_04
                    /
                   /    /file_01
                  /    /file_02
                 /four/file_03
                /    /file_04
    Having created two tarballs that contains this subtree (tarball_01.tar and tarball_02.tar) which have a content listing of (one has directory entries and the other does not):
    Code:
    bash$ tar tf tarball_01.tar     || bash$ tar tf tarball_02.tar
    ./one/file_01                   || ./
    ./one/file_02                   || ./one/
    ./one/file_03                   || ./one/file_01
    ./one/file_04                   || ./one/file_02
    ./two/file_01                   || ./one/file_03
    ./two/file_02                   || ./one/file_04
    ./two/file_03                   || ./two/
    ./two/file_04                   || ./two/file_01
    ./four/file_01                  || ./two/file_02
    ./four/file_02                  || ./two/file_03
    ./four/file_03                  || ./two/file_04
    ./four/file_04                  || ./four/
    ./three/file_01                 || ./four/file_01
    ./three/file_02                 || ./four/file_02
    ./three/file_03                 || ./four/file_03
    ./three/file_04                 || ./four/file_04
    bash$ gvim                      || ./three/
                                    || ./three/file_01
                                    || ./three/file_02
                                    || ./three/file_03
                                    || ./three/file_04
                                    || bash$
    In these cases one can get the top level directories names by doing:
    Code:
    bash$ tar tf tarball_02.tar | grep '^./[^/]*/$'
    ./one/
    ./two/
    ./four/
    ./three/
    bash$
    
    bash$ tar tf tarball_01.tar | grep '^./[^/]*/$'
    bash$
    Note that does not work for all tarballs. A better solution would be:
    bash$ tar tf tarball_02.tar | while read entry; do echo ${entry%/*}; done | sort -u
    .
    ./four
    ./one
    ./three
    ./two
    bash$

    bash$ tar tf tarball_01.tar | while read entry; do echo ${entry%/*}; done | sort -u
    ./four
    ./one
    ./three
    ./two
    bash$
    [/code]

    Now to extract a single subtree by the top-level directory:
    Code:
    bash$ tar xvf tarball_01.tar $(tar tf tarball_01.tar | grep '^./three/' | grep -v '^./three/$')
    ./three/file_01
    ./three/file_02
    ./three/file_03
    ./three/file_04
    bash$
    
    bash$ tar xvf tarball_02.tar $(tar tf tarball_02.tar | grep '^./three/' | grep -v '^./three/$')
    ./three/file_01
    ./three/file_02
    ./three/file_03
    ./three/file_04
    bash$

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •