Tar 101

2015-03-03 · 1 min read

tar is a popular tool to create and manipulate streaming archive files.

The z option specifies gzip compression and j option specifies bzip compression. It is also possible to create uncompressed archives.

Extract a tar.gz archive

tar xzf archive.tar.gz

Extract a tar.bz2/bzip archive

tar xjf archive.tar.bz2

Extract to a specific path

tar xzf archive.tar.gz -C /tmp/archive/

Extract specific files (by wildcard)

tar xzf archive.tar.gz --wildcards "*.txt"

List files from an archive

tar tzf archive.tar.gz

Create a tar.gz archive

tar czf archive.tar.gz archive/

Create a tar.bz2 archive

tar cjf archive.tar.bz2 archive/

Create an archive (with file confirmation)

tar czwf archive.tar.gz archive/

Append a file to existing archive

tar rf archive.tar file.txt

Files can only be added to plain (uncompressed) tar archives.

Backup directories at specific intervals

Add the following to your crontab.

tar czf archive-$(date +%Y%m%d).tar.gz /home/zaiste

Move a file tree between directories

tar cf - -C srcdir . | tar xpf - -C destdir

Move a file tree between machines

tar cf - * | ssh user@host "(cd /destination/path ; tar xf - )"