tar notes

From Noah.org
Jump to navigationJump to search


permission and ownership of files inside a tar

If you are making tarballs for installing on other machines you usually want the files inside the archive to be owned by root. Usually you do not build as root. You can force ownership and group with three arguments. This will record the UID and GID in the tar file as 0 (root).

tar --numeric-owner --owner=0 --group=0 -czf install_tree.tgz $STAGING_DIR

This is a fancier version that sets input and directories; trims the trailing slash off of STAGING_DIR; and sets the gzip compression level to the max.

GZIP="--best" tar --numeric-owner --owner=0 --group=0 --directory=${STAGING_DIR%/*} --exclude=.svn -czf ${OUTPUT_DIR##*/}/piebox_setup.tar.gz ${STAGING_DIR##*/}

show progress bar when decompressing tar

This displays the percentage of the tarball that is currently being uncompressed and written. I use this when writing out 2GB root filesystem images. You really need a progress bar for these things. What I do is use `gzip --list` and some `sed` to get the total uncompressed size of the tarball. From that I calculate the blocking-factor needed to divide the file into 100 parts.Finally, I print a checkpoint message for each block. For a 2GB file this gives about 10MB a block. If that is too big then you can divide the BLOCKING_FACTOR by 10 or 100, but then it's harder to print pretty output in terms of a percentage.

tar --blocking-factor=$(($(gzip --list tarball.tgz | sed -n -e "s/.*[[:space:]]\+[0-9]\+[[:space:]]\+\([0-9]\+\)[[:space:]].*$/\1/p") / 51200 + 1)) --checkpoint=1 --checkpoint-action='ttyout=Wrote %u%  \r' -zxf tarball.tgz

This is a little easier to read as a Bash shell function:

untar_progress () 
{ 
    TARBALL=$1;
    BLOCKING_FACTOR=$(($(gzip --list ${TARBALL} | sed -n -e "s/.*[[:space:]]\+[0-9]\+[[:space:]]\+\([0-9]\+\)[[:space:]].*$/\1/p") / 51200 + 1));
    tar --blocking-factor=${BLOCKING_FACTOR} --checkpoint=1 --checkpoint-action='ttyout=Wrote %u%  \r' -zxf ${TARBALL}
}

If you prefer the have each checkpoint message on a separate line then use this syntax:

tar --blocking-factor=$(($(gzip --list tarball.tgz | sed -n -e "s/.*[[:space:]]\+[0-9]\+[[:space:]]\+\([0-9]\+\)[[:space:]].*$/\1/p") / 51200 + 1)) --checkpoint=1 --checkpoint-action='echo=Wrote %u%' -zxf tarball.tgz