1

This is going to my files on the computer, but my teacher is going to need to compile it on his too. Here is the code so far. I am trying to compress three files into one TAR File.

#!/bin/bash

dd if=/dev/zero of=file.txt  bs=1701KB  count=1
dd if=/dev/zero of=filee.txt  bs=1780KB  count=1
dd if=/dev/zero of=fileee.txt  bs=1800KB  count=1

echo 'i dont really care at all about any of this' >> file.txt

echo 'just trying things out for fun' >> filee.txt

echo 'this is really boring i dont like it' >> fileee.txt

tar -czvf archive.tar.gz  /home/melwhiteastonia/file.txt /home/melwhiteastonia/filee.txt /home/melwhiteastonia/fileee.txt


gzip -l archive.tar.gz

2 Answers 2

1

Use the -C option of tar to change into the relevant working directory and specify the files by name:

tar -C /home/melwhitesastonia -cvzf archive.tar.gz file.txt filee.txt fileee.txt

Note that you may want to specify an absolute path to the archive (e.g., $PWD/archive.tar.gz) if you don't want it in the same directory. Also note that you don't need to gzip it because the -z flag to tar already does that.

In addition, you may wish to place your files in a suitably named directory instead of placing them directly in the root of the archive, since that is customary and nicer to users. You would do that by specifying the directory name instead of the files and leaving off that directory component from the -C option.

2
  • This doesn't work if you refer to *.txt using an absolute path like $HOME/file.txt. Any solution to that? Commented May 1, 2020 at 19:39
  • You can use GNU tar's --transform option. The tar from libarchive has a similar option. Commented May 1, 2020 at 19:59
1

With the argument --transform you could do something like that

tar -czv --transform 's:home/melwhiteastonia/::g' -f archive.tar.gz \
    /home/melwhiteastonia/file.txt \
    /home/melwhiteastonia/filee.txt \
    /home/melwhiteastonia/fileee.txt

Regex is home/melwhiteastonia/ because tar removes leading / anyway.

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.