How to create tar archive split into, or spanning, multiple files?

27

13

According to this page, one can let tar create a tar archive "split" into 100 Mb files:

tar -c -M --tape-length=102400 --file=disk1.tar largefile.tgz

The problem is that this command will require you to interactively give a new filename for the next file, after the first file is filled.

Anybody knows of a way to skip this interactive step, and let tar do the "splitting" automatically?

Samuel Lampa

Posted 2011-05-31T13:48:51.053

Reputation: 525

Related to this: https://unix.stackexchange.com/q/61774/6860

– sampablokuper – 2018-03-15T13:31:04.013

Answers

20

Take a look at the --new-volume-script option, which lets you replace the prompting mechanism with a different mechanism or with a generated filename. ((tar.info)Multi-Volume Archives in the tar info page.) The problem with split is that you need to cat the pieces back together to do anything, whereas a multivolume archive should be a bit more flexible.

geekosaur

Posted 2011-05-31T13:48:51.053

Reputation: 10 195

The problem with this is that it's overly involved nonsense and promotes the opposite of proper Unix style apps. – Jan Kyu Peblik – 2019-03-17T20:04:42.597

That's true, of course. – Eduardo I. – 2011-05-31T14:00:07.210

2

Thanks, this is what I was looking for! I now found out that there is actually some instructions (incl. example) available here: http://www.gnu.org/software/tar/manual/tar.html#Using-Multiple-Tapes

– Samuel Lampa – 2011-06-01T09:17:05.373

33

You can use split for this:

tar czpvf - /path/to/archive | split -d -b 100M - tardisk

This tells tar to send the data to stdout, and split to pick it from stdin - additionally using a numeric suffix (-d), a chunk size (-b) of 100M and using 'tardisk' as the base for the resulting filenames (tardisk00, tardisk01, tardisk02, etc.).

To extract the data afterwards you can use this:

cat tardisk* | tar xzpvf -

Eduardo I.

Posted 2011-05-31T13:48:51.053

Reputation: 495

1Small correction, -d is for numeric suffix, not prefix. – yclian – 2014-12-23T13:16:01.553

8

Of course the best option to use is the --new-volume-script option.

But, if you know the size of the file (in this case, largefile.tgz), then you can do this also:

tar -c -M -L 102400 --file=disk1.tar --file=disk2.tar --file=disk3.tar largefile.tgz

Summary:

-c = Create
-M = multi-volume
-L 102400 = 100MB files (disk1.tar, disk2.tar, disk3.tar ...)

(For the -L, specify as many as needed so that the total sum of the tar files is larger than largefile.tgz)

If you are trying to tar a directory tree structure

user1260486

Posted 2011-05-31T13:48:51.053

Reputation: 189

1

I got it to work with the following commands:

mkdir -p ./split
rm -rf ./split/*
tar -cML 102400 -F 'cp "${TAR_ARCHIVE}" \
    ./split/part_${TAR_VOLUME}.tar' \
    -f split/part_1.tar large_file.tar.gz

The only problem is that part_1.tar will actually be the last file, and the others are shifted by one. I.e. part_2.tar is actually the first part, and part_k.tar is the (n - 1)th part. Fixing this with some shell script is trivial, and left as an exercise for the reader.

strupo

Posted 2011-05-31T13:48:51.053

Reputation: 17

1

it will automatically create files of size 1.1GB, if your tar is bigger in size, you can increase the number, for an example 1000 {2..1000} or you can increase the input to tape-length argument.

tar --tape-length=1048576 -cMv --file=tar_archive.{tar,tar-{2..100}} backup.tar.lzma

GraphicalDot

Posted 2011-05-31T13:48:51.053

Reputation: 111