List tar contents only first level, then subdirectory & extract

Discussion in 'Command Line' started by postcd, Jul 27, 2014.

  1. postcd

    postcd Member

    Messages:
    59
    Likes Received:
    5
    Trophy Points:
    8
    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

  2. ryanvade

    ryanvade Administrator Staff Member Staff Writer

    Messages:
    1,239
    Likes Received:
    418
    Trophy Points:
    83
    A GUI archive tool would do this nicely.
  3. postcd

    postcd Member

    Messages:
    59
    Likes Received:
    5
    Trophy Points:
    8
    thats a good idea, but its a remote plain linux server for business purpose, i dont much want to experiment and install VNC & desktop on it.. Would prefer linux command line commands.
  4. rstanley

    rstanley Member

    Messages:
    64
    Likes Received:
    42
    Trophy Points:
    18
    It took some research, but I think I found what you are looking for.

    If the zip file is archive.tar.gz, and you have the following directories (Containing files and other directories), in the archive: foo/bar/xyz

    To view the root directory of the archive:
    Code:
    tar --list --verbose --no-recursion --file=archive.tar.gz  foo/*
    To view the contents of 'foo/bar/'
    Code:
    tar --list --verbose --no-recursion --file=archive.tar.gz  foo/bar/*
    etc, etc, etc...

    I tried to use the shortened versions of these options, but couldn't get them to work. You can shorten it to this:
    Code:
    tar tv --no-recursion -f archive.tar.gz foo/*
    I had a typo in my test! ;^)

    You will need to know the root directory of the archive depending on how it was created, in this case, /foo, (or in other archives, ./ if the archive was created starting with the current directory)

    I hope this works for you!
    Last edited: Jul 28, 2014
    JasKinasis and ryanvade like this.
  5. JasKinasis

    JasKinasis Member

    Messages:
    51
    Likes Received:
    27
    Trophy Points:
    18
    To answer the other part of the OPs question, e.g. how to extract a particular sub-directory or file from the archive; after you have identified the items you want to extract using the steps in rstanley's post, you can extract a single file like this:
    Code:
    tar xvf /path/to/archive.tar.gz directory/sub-directory/filename.ext
    Where /path/to/archive.tar.gz is the path and filename of the archive file and directory/sub-directory/filename.ext is the path and filename of the target file you want to extract from the archive. As rstanley has noted, depending on how the archive was created, you might need to prepend the path to the target file with ./

    To extract an entire sub-directory, you specify the path to the sub-directory you want to extract:
    Code:
    tar xvf /path/to/archive.tar.gz directory/sub-dir/dir_to_extract/
    Again, /path/to/archive.tar.gz is the path to the archive and directory/sub-dir/dir_to_extract/ is the path to the subdirectory you want to extract. Again, if necessary, prepend the path to the target sub-directory with ./
  6. postcd

    postcd Member

    Messages:
    59
    Likes Received:
    5
    Trophy Points:
    8
    This command DID NOT worked:
    but this command WORKED:
    but alas it shows files recursivelly, which i do NOT want :-(

    example shows (amongst others):
    when i browsed ./root/*
  7. JasKinasis

    JasKinasis Member

    Messages:
    51
    Likes Received:
    27
    Trophy Points:
    18
    I think it's got something to do with the wildcard you are using. I think it is causing tar to look for everything inside /root/ and is overriding the --no-recursion flag.

    Actually, from looking at the man page for tar (on Cygwin - at work ATM!), it says that the --no-recursion option only applies to the c, r and u modes. So it doesn't look as if --no-recursion will work properly when listing archives anyway.

    I've noticed that if you use --no-recursion without the wildcard:
    Code:
    tar tv --no-recursion -f vzdump-1050.tgz ./root/
    It will just list the ./root/ directory, but will not show any other items that are in the directory or any sub-directories. So it will literally just list the directory, but none of its contents and none of its sub-directories.

    NOTE: In the above command I didn't use your file, I just substituted your filename and search directory with the ones I actually used in my tests!

    Whereas without the --no-recursion flag:
    Code:
    tar tvf vzdump-1050.tgz ./root/
    You get exactly the same results as
    Code:
    tar tvf vzdump-1050.tgz ./root/*
    Which literally just lists everything in ./root/ and all sub-directories. Which again, isn't what you want!

    Really not sure what to suggest. :/
    You could perhaps try piping the output from tar into something like grep to try to filter/narrow the results a little bit (assuming you know what you are looking for). And/or perhaps also pipe the output to less to allow you to manually scroll though the results a little more easily. Might help you to actually narrow in on the parts that you want to see. Other than that I'm rapidly running out of ideas!
    rstanley likes this.
  8. rstanley

    rstanley Member

    Messages:
    64
    Likes Received:
    42
    Trophy Points:
    18
    What system are you running, and what version of tar? (tar --version) I am running Debian Jessie (Testing), with GNU tar version 1.27.1.

    I don't know if taring with one version of tar, and untarring with another is part of the problem or not. It may be time for a more modern tar replacement.
  9. postcd

    postcd Member

    Messages:
    59
    Likes Received:
    5
    Trophy Points:
    8
    thx, im having:
    CentOS release 5.10 (Final) .. redhat
    tar (GNU tar) 1.15.1
    yum update/upgrade? ... No Packages marked for Update
  10. rstanley

    rstanley Member

    Messages:
    64
    Likes Received:
    42
    Trophy Points:
    18
    From http://www.gnu.org/software/tar/ :
    version 1.15.1 - 2004-12-21
    version 1.27.1 - 2013-11-17

    There have been 16 versions of tar since your version, and another beyond my current version. It would be almost impossible for us to give you further assistance due to the huge discrepancy of the features and bugfixes between the two versions.

    As @JasKinasis said:
    You could also use one or more of the --exclude options along with the grep, and list commands.

    I would not advise attempting to upgrade the version of tar on CentOS, as it could lead to other problems of dependency. The only other suggestion would be to use another computer with a newer version of Linux, such as Debian Jessie (Testing) to extract, and then scp (SSH) the files over to the final server destination.

    It will be a lot of work. I wish you all the best with this apparently major job.
    JasKinasis and ryanvade like this.
  11. ryanvade

    ryanvade Administrator Staff Member Staff Writer

    Messages:
    1,239
    Likes Received:
    418
    Trophy Points:
    83
    tar 1.28.1 is now out. ;)
    JasKinasis likes this.

Share This Page