1

I'm looking for a command line tool that takes a tarball, uncompresses and unpacks it, and turns the contents into one contiguous file, comprised of lines that are a maximum of eighty characters wide, so that it can be mailed as plain text without corruption.

Thank you for pointing me to such tool.

Thanks.

  • Tarball can be mailed too (w/o any problem). Will you accept such answer? – Romeo Ninov Dec 23 '23 at 07:57
  • 2
    An arbitrary file cannot be embedded as-is in e-mail reliably, that's why base64 is used to encode attachments. If you insist on having an "attachment" in the actual body, you can base64 the tarball by yourself, the recipient will need to base64 -d it by hand. E-mail clients do this automatically with actual attachments and it works. Do not unpack the tarball beforehand because it takes care of names and boundaries of the files within, let the recipient do this. So the easiest way is just to attach the tarball. What problem are you trying to solve by willing to invent another way? – Kamil Maciorowski Dec 23 '23 at 09:14
  • 2
    It seems that it would be better to use a MIME-aware mailer client and just attach the tarball – Chris Davies Dec 23 '23 at 10:07
  • This feels like an XY problem. The requested solution won't work for binary files and will corrupt text files too. In the early days of email, uuencode and split were created specifically to email compressed tarballs. Now you can just attach them and the MIME format will take care of what uuencode use to do, but you still might need to split it first so the email isn't too big. – user10489 Dec 23 '23 at 10:54
  • Consider the process, with a "tarball, uncompresses and unpacks it, and turns the contents into one contiguous file" - (1) a tarball already is one contiguous file (2) you want to make the content larger by uncompressing it? – Chris Davies Dec 23 '23 at 11:35
  • 1
    Looks like you want to be using shar instead of tar. – Toby Speight Dec 23 '23 at 14:07
  • That's an interesting prospect, @TobySpeight. Didn't that largely go out along with uuencode and friends? – Chris Davies Dec 23 '23 at 17:51
  • Thank you for your answers. In the case of plain text input, which of the above solutions makes the text in the email actually readable (even if a bit disrupted by the encoding)? Thanks. – Joselin Jocklingson Jan 02 '24 at 21:37

2 Answers2

0

This is an answer - and may do what you want:

tar xf tarball-test.tar.gz --to-stdout | fold -w 80 -s > output.txt

alternative:

tar zxOf tarball-test.tar.gz | fold -w 80 -s > output.txt

I used GNU tar for this example, but one of the incantations above should work with other tar implementations.

In fold note that the -s option breaks at spaces - if they exist. See this Q&A for alternatives & options to fold.

You can test this by creating some files & making a tarball:

$ for i in {1..25}; do printf 'file1' >> file1.txt; done
$ for i in {1..30}; do printf 'xxfile2' >> file2.txt; done
$ for i in {1..20}; do printf 'file3 ' >> file3.txt; done
$ tar -czf tarball-test.tar.gz file1.txt file2.txt file3.txt

Note that the files created above contain no line breaks. This is unusual, but I didn't want to make assumptions re the OP's files. However, usage of such (non-delimited) files makes the behavior of fold undefined. Otherwise, to avoid this risk, create the files with line breaks as shown below.

$ for i in {1..25}; do printf 'file1' >> file1.txt; done; printf '\n' >> file1.txt
$ for i in {1..30}; do printf 'xxfile2' >> file2.txt; done; printf '\n' >> file2.txt
$ for i in {1..20}; do printf 'file3 ' >> file3.txt; done; printf '\n' >> file3.txt
$ tar -czf tarball-test.tar.gz file1.txt file2.txt file3.txt
Seamus
  • 2,925
0

It does as requested, save this to concat_mailer.sh

#!/bin/bash

extract .tar file containing .txt--merge, format, e-mail

time_stamp=$(date +"%Y-%m-%d_%H:%M:%S") cat_file="concat_[$time_stamp]"

tar -x -f "$1"

create single plain text file

for text in *.txt do cat "$text" >> "$cat_file.txt" done

delimited text captured for output

cat "$cat_file.txt" | fold -w 80 > temp.txt

mv history.log to_append.txt echo -e "[ "$time_stamp" ] "$1"\n" > history.log cat temp.txt >> history.log cat to_append.txt >> history.log

text piped to email body, via stdin

cat temp.txt | msmtp -a default -t target@email.address

if [ $? != 0 ]; then echo -e "\n\033[31mFailed.\033[m\n" else echo -e "\n\033[32mSuccess.\033[m\n" fi

rm *.txt

exit 0

Run this (makes script executable):
chmod +x concat_mailer.sh
You only have to do this once.

Usage: ./concat_mailer.sh your_file.tar
Tested working on Fedora 38.

Making the mail command work is harder than I expected.
I ended up making this helper script for msmtp HERE.

Check your path's to mail & sendmail & msmtp by typing:

whereis mail; whereis sendmail; whereis msmtp

My sendmail happened to be in /usr/sbin/. It varies. Change these to your path's, respectively:

sudo ln -f -s /usr/sbin/sendmail /usr/bin/mail

sudo ln -f -s /usr/bin/msmtp /usr/sbin/sendmail

Now ,mail & sendmail will use msmtp as their MTP service.