Read concatenated output of tgz archive (with plain text files), with filenames, without unpacking in bash?

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
2
down vote

favorite
1












I have a bunch of text files/logs, which I'd like to pack into a gzipped tar archive, say:



cd /tmp
echo "My Content One" > aaa.log
echo "My Content OneA" >> aaa.log
echo "My Content Two" > bbb.log
echo "My Content TwoB" >> bbb.log
tar czvf test.tgz *.log


Now, if I open this with less test.tgz, I get a listing of files same as tar tzvf test.tgz would give me:



-rw-rw-r-- user/user 31 2017-10-29 03:10 aaa.log
-rw-rw-r-- user/user 31 2017-10-29 03:10 bbb.log


First, I found https://www.serverwatch.com/tutorials/article.php/3798511/Reading-Compressed-Files-With-less.htm, and tried setting up in my ~/.bashrc:



 alias tgzless='LESSOPEN="|tar --to-stdout -zxf %s" less'


Now I can do tgzless test.tgz, and this prints the concatenated contents of all files:



My Content One
My Content OneA
My Content Two
My Content TwoB


... but unfortunately doesn't print the filenames, so I don't really know which part belongs where.



Then, I found How can i view the contents of a tar.gz file (filenames + filesize), and tried this command:



gzip -dc test.tgz | less


Now, this will print both a file heading, and the content of the file - unfortunately the file heading is binary and difficult to read - it looks something like this inside less:



aaa.log^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@
^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@
^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@0000664^@0001750^@0001750^@00000000037^@131752
34246^@014636^@ 0^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@
^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@
^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@ustar
^@user^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@user^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@
^@^@^@^@^@^@^@
^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@
^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@
^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@
^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@
^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@My Content One
My Content OneA
^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@
...


... so it is hard to read (note that gzip -dc test.tgz | tar -tf - | less gives just a listing of filenames, though less verbose one than tar tzvf)



So can I set up an alias somehow using these standard commands, that would print all filename contents in a tar.gz archive, such that first the filename of the file is printed (preferably in tar tzvf format, with username, permission and timestamp), and then its contents?







share|improve this question


























    up vote
    2
    down vote

    favorite
    1












    I have a bunch of text files/logs, which I'd like to pack into a gzipped tar archive, say:



    cd /tmp
    echo "My Content One" > aaa.log
    echo "My Content OneA" >> aaa.log
    echo "My Content Two" > bbb.log
    echo "My Content TwoB" >> bbb.log
    tar czvf test.tgz *.log


    Now, if I open this with less test.tgz, I get a listing of files same as tar tzvf test.tgz would give me:



    -rw-rw-r-- user/user 31 2017-10-29 03:10 aaa.log
    -rw-rw-r-- user/user 31 2017-10-29 03:10 bbb.log


    First, I found https://www.serverwatch.com/tutorials/article.php/3798511/Reading-Compressed-Files-With-less.htm, and tried setting up in my ~/.bashrc:



     alias tgzless='LESSOPEN="|tar --to-stdout -zxf %s" less'


    Now I can do tgzless test.tgz, and this prints the concatenated contents of all files:



    My Content One
    My Content OneA
    My Content Two
    My Content TwoB


    ... but unfortunately doesn't print the filenames, so I don't really know which part belongs where.



    Then, I found How can i view the contents of a tar.gz file (filenames + filesize), and tried this command:



    gzip -dc test.tgz | less


    Now, this will print both a file heading, and the content of the file - unfortunately the file heading is binary and difficult to read - it looks something like this inside less:



    aaa.log^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@
    ^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@
    ^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@0000664^@0001750^@0001750^@00000000037^@131752
    34246^@014636^@ 0^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@
    ^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@
    ^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@ustar
    ^@user^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@user^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@
    ^@^@^@^@^@^@^@
    ^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@
    ^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@
    ^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@
    ^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@
    ^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@My Content One
    My Content OneA
    ^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@
    ...


    ... so it is hard to read (note that gzip -dc test.tgz | tar -tf - | less gives just a listing of filenames, though less verbose one than tar tzvf)



    So can I set up an alias somehow using these standard commands, that would print all filename contents in a tar.gz archive, such that first the filename of the file is printed (preferably in tar tzvf format, with username, permission and timestamp), and then its contents?







    share|improve this question
























      up vote
      2
      down vote

      favorite
      1









      up vote
      2
      down vote

      favorite
      1






      1





      I have a bunch of text files/logs, which I'd like to pack into a gzipped tar archive, say:



      cd /tmp
      echo "My Content One" > aaa.log
      echo "My Content OneA" >> aaa.log
      echo "My Content Two" > bbb.log
      echo "My Content TwoB" >> bbb.log
      tar czvf test.tgz *.log


      Now, if I open this with less test.tgz, I get a listing of files same as tar tzvf test.tgz would give me:



      -rw-rw-r-- user/user 31 2017-10-29 03:10 aaa.log
      -rw-rw-r-- user/user 31 2017-10-29 03:10 bbb.log


      First, I found https://www.serverwatch.com/tutorials/article.php/3798511/Reading-Compressed-Files-With-less.htm, and tried setting up in my ~/.bashrc:



       alias tgzless='LESSOPEN="|tar --to-stdout -zxf %s" less'


      Now I can do tgzless test.tgz, and this prints the concatenated contents of all files:



      My Content One
      My Content OneA
      My Content Two
      My Content TwoB


      ... but unfortunately doesn't print the filenames, so I don't really know which part belongs where.



      Then, I found How can i view the contents of a tar.gz file (filenames + filesize), and tried this command:



      gzip -dc test.tgz | less


      Now, this will print both a file heading, and the content of the file - unfortunately the file heading is binary and difficult to read - it looks something like this inside less:



      aaa.log^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@
      ^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@
      ^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@0000664^@0001750^@0001750^@00000000037^@131752
      34246^@014636^@ 0^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@
      ^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@
      ^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@ustar
      ^@user^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@user^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@
      ^@^@^@^@^@^@^@
      ^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@
      ^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@
      ^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@
      ^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@
      ^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@My Content One
      My Content OneA
      ^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@
      ...


      ... so it is hard to read (note that gzip -dc test.tgz | tar -tf - | less gives just a listing of filenames, though less verbose one than tar tzvf)



      So can I set up an alias somehow using these standard commands, that would print all filename contents in a tar.gz archive, such that first the filename of the file is printed (preferably in tar tzvf format, with username, permission and timestamp), and then its contents?







      share|improve this question














      I have a bunch of text files/logs, which I'd like to pack into a gzipped tar archive, say:



      cd /tmp
      echo "My Content One" > aaa.log
      echo "My Content OneA" >> aaa.log
      echo "My Content Two" > bbb.log
      echo "My Content TwoB" >> bbb.log
      tar czvf test.tgz *.log


      Now, if I open this with less test.tgz, I get a listing of files same as tar tzvf test.tgz would give me:



      -rw-rw-r-- user/user 31 2017-10-29 03:10 aaa.log
      -rw-rw-r-- user/user 31 2017-10-29 03:10 bbb.log


      First, I found https://www.serverwatch.com/tutorials/article.php/3798511/Reading-Compressed-Files-With-less.htm, and tried setting up in my ~/.bashrc:



       alias tgzless='LESSOPEN="|tar --to-stdout -zxf %s" less'


      Now I can do tgzless test.tgz, and this prints the concatenated contents of all files:



      My Content One
      My Content OneA
      My Content Two
      My Content TwoB


      ... but unfortunately doesn't print the filenames, so I don't really know which part belongs where.



      Then, I found How can i view the contents of a tar.gz file (filenames + filesize), and tried this command:



      gzip -dc test.tgz | less


      Now, this will print both a file heading, and the content of the file - unfortunately the file heading is binary and difficult to read - it looks something like this inside less:



      aaa.log^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@
      ^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@
      ^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@0000664^@0001750^@0001750^@00000000037^@131752
      34246^@014636^@ 0^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@
      ^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@
      ^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@ustar
      ^@user^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@user^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@
      ^@^@^@^@^@^@^@
      ^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@
      ^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@
      ^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@
      ^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@
      ^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@My Content One
      My Content OneA
      ^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@
      ...


      ... so it is hard to read (note that gzip -dc test.tgz | tar -tf - | less gives just a listing of filenames, though less verbose one than tar tzvf)



      So can I set up an alias somehow using these standard commands, that would print all filename contents in a tar.gz archive, such that first the filename of the file is printed (preferably in tar tzvf format, with username, permission and timestamp), and then its contents?









      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 19 '17 at 14:30









      Jeff Schaller

      32.1k849109




      32.1k849109










      asked Oct 29 '17 at 2:23









      sdaau

      2,54763047




      2,54763047




















          1 Answer
          1






          active

          oldest

          votes

















          up vote
          2
          down vote













          Assuming you’re using GNU tar, create a file named tarcat on your path with the following contents:



          #!/bin/sh

          # Permissions user/group size date time name
          printf "%s %s/%s %7d %s %sn" "$TAR_MODE" "$TAR_UNAME" "$TAR_GNAME" "$TAR_SIZE" "$(date -d @$TAR_ATIME)" "$TAR_FILENAME"

          exec cat


          Then run tar x --to-command=tarcat -f test.tgz | less:



          0664 user/user 31 Sun 29 Oct 03:10:00 UTC 2017 aaa.log
          My Content One
          My Content OneA
          0664 user/user 31 Sun 29 Oct 03:10:00 UTC 2017 bbb.log
          My Content Two
          My Content TwoB


          Decoding the octal file mode and adapting the date/time output is left as an exercise for the reader.






          share|improve this answer




















            Your Answer







            StackExchange.ready(function()
            var channelOptions =
            tags: "".split(" "),
            id: "106"
            ;
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function()
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled)
            StackExchange.using("snippets", function()
            createEditor();
            );

            else
            createEditor();

            );

            function createEditor()
            StackExchange.prepareEditor(
            heartbeatType: 'answer',
            convertImagesToLinks: false,
            noModals: false,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: null,
            bindNavPrevention: true,
            postfix: "",
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            );



            );













             

            draft saved


            draft discarded


















            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f401145%2fread-concatenated-output-of-tgz-archive-with-plain-text-files-with-filenames%23new-answer', 'question_page');

            );

            Post as a guest






























            1 Answer
            1






            active

            oldest

            votes








            1 Answer
            1






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes








            up vote
            2
            down vote













            Assuming you’re using GNU tar, create a file named tarcat on your path with the following contents:



            #!/bin/sh

            # Permissions user/group size date time name
            printf "%s %s/%s %7d %s %sn" "$TAR_MODE" "$TAR_UNAME" "$TAR_GNAME" "$TAR_SIZE" "$(date -d @$TAR_ATIME)" "$TAR_FILENAME"

            exec cat


            Then run tar x --to-command=tarcat -f test.tgz | less:



            0664 user/user 31 Sun 29 Oct 03:10:00 UTC 2017 aaa.log
            My Content One
            My Content OneA
            0664 user/user 31 Sun 29 Oct 03:10:00 UTC 2017 bbb.log
            My Content Two
            My Content TwoB


            Decoding the octal file mode and adapting the date/time output is left as an exercise for the reader.






            share|improve this answer
























              up vote
              2
              down vote













              Assuming you’re using GNU tar, create a file named tarcat on your path with the following contents:



              #!/bin/sh

              # Permissions user/group size date time name
              printf "%s %s/%s %7d %s %sn" "$TAR_MODE" "$TAR_UNAME" "$TAR_GNAME" "$TAR_SIZE" "$(date -d @$TAR_ATIME)" "$TAR_FILENAME"

              exec cat


              Then run tar x --to-command=tarcat -f test.tgz | less:



              0664 user/user 31 Sun 29 Oct 03:10:00 UTC 2017 aaa.log
              My Content One
              My Content OneA
              0664 user/user 31 Sun 29 Oct 03:10:00 UTC 2017 bbb.log
              My Content Two
              My Content TwoB


              Decoding the octal file mode and adapting the date/time output is left as an exercise for the reader.






              share|improve this answer






















                up vote
                2
                down vote










                up vote
                2
                down vote









                Assuming you’re using GNU tar, create a file named tarcat on your path with the following contents:



                #!/bin/sh

                # Permissions user/group size date time name
                printf "%s %s/%s %7d %s %sn" "$TAR_MODE" "$TAR_UNAME" "$TAR_GNAME" "$TAR_SIZE" "$(date -d @$TAR_ATIME)" "$TAR_FILENAME"

                exec cat


                Then run tar x --to-command=tarcat -f test.tgz | less:



                0664 user/user 31 Sun 29 Oct 03:10:00 UTC 2017 aaa.log
                My Content One
                My Content OneA
                0664 user/user 31 Sun 29 Oct 03:10:00 UTC 2017 bbb.log
                My Content Two
                My Content TwoB


                Decoding the octal file mode and adapting the date/time output is left as an exercise for the reader.






                share|improve this answer












                Assuming you’re using GNU tar, create a file named tarcat on your path with the following contents:



                #!/bin/sh

                # Permissions user/group size date time name
                printf "%s %s/%s %7d %s %sn" "$TAR_MODE" "$TAR_UNAME" "$TAR_GNAME" "$TAR_SIZE" "$(date -d @$TAR_ATIME)" "$TAR_FILENAME"

                exec cat


                Then run tar x --to-command=tarcat -f test.tgz | less:



                0664 user/user 31 Sun 29 Oct 03:10:00 UTC 2017 aaa.log
                My Content One
                My Content OneA
                0664 user/user 31 Sun 29 Oct 03:10:00 UTC 2017 bbb.log
                My Content Two
                My Content TwoB


                Decoding the octal file mode and adapting the date/time output is left as an exercise for the reader.







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Oct 29 '17 at 13:41









                Stephen Kitt

                144k22312377




                144k22312377



























                     

                    draft saved


                    draft discarded















































                     


                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function ()
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f401145%2fread-concatenated-output-of-tgz-archive-with-plain-text-files-with-filenames%23new-answer', 'question_page');

                    );

                    Post as a guest













































































                    Popular posts from this blog

                    How to check contact read email or not when send email to Individual?

                    Displaying single band from multi-band raster using QGIS

                    How many registers does an x86_64 CPU actually have?