bsdtar and xz on one line

Software

Asahina Mikuru worried at the prospect of compressing and archiving in the same line?

Starting with OS X Mavericks, we have bsdtar that allows for one line creation of tar.xz files, just as we have with bz2 and gz.

Aside: In much the same way Nagato Yuki began gracing computer hardware posts here, in 2007 Asahina Mikuru began accompanying compression posts. I think I was implying her Mikuru Beam was more effective than regular file compression algorithms. She appears nervous; I can only asusme it’s due to her unfamiliarity with compressing and archiving in the one command

Previously, to archive and compress a folder with xz, we would do this:

% tar cvf archive.tar folder/
% xz -9 archive.tar

Now we can just do this:

% tar cJvf archive.tar.xz folder/

Boom! There’s just one catch. This uses xz with its default compression level, not the 9 I use for archiving purposes. Fortunately, we can also specify this:

% tar cJvf --options xz:compression-level:9 archive.tar.xz folder/

Okay, I hear you say, what’s the point of this monsterous one liner? The two lines we had previously may have had fewer characters! Unlike my overworked pencils from my last round of exams, you have a point.

Among many reasons why its great, it means we can create an alias in our shell. I still use tcshrc, so I went ahead and added this to my .tcshrc:

alias tz 'tar cvJf --options xz:compression-level=9'

My bash knowledge is as corroded as my overworked pencils (does fake graphite corrode?), but here’s the equivlient:

alias tz='tar cvJf --options xz:compression-level=9'

Now whenever I want to create an xz tar archive with its maximum compression ratio, I can just do this:

% tz archive.tar.xz folder/

Now we just need pax to support xz along with bzip2 and gz and we’ll be set.

Author bio and support

Me!

Ruben Schade is a technical writer and IaaS engineer in Sydney, Australia who refers to himself in the third person in bios. Wait, not BIOS… my brain should be EFI by now.

The site is powered by Hugo, FreeBSD, and OpenZFS on OrionVM, everyone’s favourite cloud infrastructure provider.

If you found this post helpful or entertaining, you can shout me a coffee or buy some silly merch. Thanks!