atool: handling archives without headaches

December 28th, 2008 edited by Vicho

Article submitted by Paulus Esterhazy. Last article of 2008! We hope 2009 will be full of good articles about Debian and Ubuntu packages. But we can’t do it without your help, please submit good articles about software you like!

Have you ever wrestled with tar(1) and other Unix archive tools? Wondered why every tool has its own arcane syntax and nonstandard behavior? And why on earth is it impossible to use unzip(1) to unpack multiple archive files?

The good news is that, in the Unix universe, you can be sure that someone else has asked himself the same question before and, perhaps, solved it. And so it is. The atool package supplies a set of commands that hide the complexities and lets you work with compressed file archives in a sensible manner.

Arguably the most useful commands included are apack, aunpack and als which as their names suggest create an archive, and extract or list its contents. In addition, acat uncompresses an archive file and outputs the file contents to standard output, whereas adiff compares two archives and shows the differences between their contents. These commands work as you would expect them to, and the author has stuck to the Unix conventions where possible.

The details, however, are worth a look. Some examples:

  • aunpack archive.tgz Unpacks all the files in the archive. If the author of the archive was so inconsiderate as to put multiple files in the archive’s root, the command automatically creates a directory and moves the files inside.
  • aunpack -e archive1.tgz Unpacks each archive.
  • apack archive.tar.bz2 *.txt Creates a new compressed archive containing all text files in the current working directory.
  • als archive.rar Shows the names of the files contained in the archive.

Note that for each atool command the archive file name precedes the names of the files to add or extract on the command line. Compare aunpack -e archive1.tgz archive2.tgz and aunpack archive1.tgz file.txt.

As you can see, atool commands automatically determine the file type by looking at the extension, but they resort to using file(1) if the simpler heuristic fails (you can override the guess using the -F switch). Most commonly used archive types are supported, including tar+gzip, tar+bzip2, zip and rar; a notable omission in the version available in Debian Sarge and Ubuntu 8.04 is the relatively new LZMA compression format (lzma(1)), but the active upstream author has already added support for it. You can also extract a .deb package by forcing the ar archiving method using the switch -F a.

Atool is blessed with the virtue of simplicity and its options are explained in the helpful manpage, which thankfully doesn’t follow the Unix convention of leaving out examples. Here’s one last gem from the documentation. If you frequently work with archives you get from the internet, you probably follow this procedure: Check archive type, check that the archive contains a top-level directory, unpack the archive, change to the directory extracted. These steps can be combined by adding the following function definition to your $HOME/.bashrc or $HOME/.zshrc:

aunpack () {
  TMP=$(mktemp /tmp/aunpack.XXXXXXXXXX)
  atool -x --save-outdir=$TMP "$@"
  DIR="$(cat $TMP)"
  [ "$DIR" != "" -a -d "$DIR" ] && cd “$DIR”
  rm $TMP

After adding these lines, you can “reload” the configuration file in your shell using source ~/.bashrc or source ~/.zshrc. Now running aunpack automatically changes the current directory to the one just extracted. Note that adding this snippet is necessary to achieve the desired behavior becausing a directory change is effectively useless unless it is performed in the context of the running shell.

Atool was written in perl by Oskar Liljeblad. It is available in all current Debian and Ubuntu releases. Besides atool, there are a few other tools that aspire to be the Swiss army knife of archivers, for example deco. These programs, however, are not as full-featured and mature as atool.

Posted in Debian, Ubuntu |

8 Responses

  1. Martin Says:

    i was just thinking about this problem lately, so there´s no need to write something myself.

    so thanks a lot!!!

  2. Vadim P. Says:

    Thanks a ton. I was preferring .zips lately because of the human syntax.

  3. Carlo Says:

    Thank you very much. This is a very nice tool I wasn’t aware of.

  4. Dotan Cohen Says:

    Thank you, this is one of the best posts on debaday that I remember ever!

  5. Hong Leong Says:

    For unpacking archives, regardless of format, I find unp — a shell frontend for uncompressing/unpacking tools — to be very useful.

    Thanks Vicho for sharing about atools. Gonna check it out now. :)

  6. HoverHell Says:

    Hm… ‘aunpack’ tries to find ‘rar’ executable, while ‘unp’ just finds ‘unrar’. Quite a problem…

  7. Krzysztof Sobolewski Says:

    Extremely useful stuff. I will now try hard to overcome the NIH syndrome and abandon usage of my home-brew “unwhatever” script ;)

  8. Ged Haywood Says:

    That’s rather good. Thank you.