Skip to Content

Converting CPIO Files to Tarballs

Posted on One min read

I needed to convert a directory full of CPIO files to tar balls. This quick script did the trick for me but didn’t preserve the user / group. Running it as root will preserve the ownership information but that wasn’t important for my immediate use case.

#!/bin/bash

SRC_DIR=$(pwd)
for i in *.cpio; do
  CPIO_TMP_DIR="$(mktemp -d /tmp/cpioconv.XXXX)"
  (cd ${CPIO_TMP_DIR} && cpio -idm < "${SRC_DIR}/${i}" && tar -cf ${SRC_DIR}/${i%%.cpio}.tar .)
  rm -rf ${CPIO_TMP_DIR}
done