Thursday, April 14, 2011

[tip] Transform file or directory names during the extraction of tarball

Suppose we have a tarball containing a directory structure of root filesystem for an embedded device. ex:
  rootfs/
  rootfs/init.rc
  rootfs/dev/...
  rootfs/etc/...
  rootfs/usr/...
We'd like to extract the contained directory structure under the rootfs/ directly into the root directory of a mounted partition, for example, /mnt/sdb1. Otherwise, we will have to deal with the relocation of the device node files(rootfs/dev/...), which is a little bit cumbersome.

To skip the containing folder 'rootfs/', we can specify the --transform option during the extracting process,
tar xvfj ~/rootfs.tar.bz2 -C /mnt/sdb1/ --transform 's/rootfs//'
and yield the following extracted directory structure.
  /mnt/sdb1/
  /mnt/sdb1/init.rc
  /mnt/sdb1/dev/...
  /mnt/sdb1/etc/...
  /mnt/sdb1/usr/...