2

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?

2

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 %s\n" "${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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.