BSD Tar Cheatsheet #
BSD tar is the version available on BSD-based systems and has some differences from GNU tar.
Basic Commands #
Command/Option | Example | Description |
---|---|---|
tar -cf | tar -cf archive.tar file1 file2 | Create a new archive file (-c for create, -f for file) |
tar -xf | tar -xf archive.tar | Extract an archive file (-x for extract, -f for file) |
tar -tf | tar -tf archive.tar | List the contents of an archive file (-t for list, -f for file) |
tar -czf | tar -czf archive.tar.gz file1 file2 | Create a compressed archive with gzip (-z for gzip compression) |
tar -xzf | tar -xzf archive.tar.gz | Extract a gzip compressed archive (-z for gzip compression) |
tar -jcf | tar -jcf archive.tar.bz2 file1 file2 | Create a compressed archive with bzip2 (-j for bzip2 compression) |
tar -xjf | tar -xjf archive.tar.bz2 | Extract a bzip2 compressed archive (-j for bzip2 compression) |
tar -Jcf | tar -Jcf archive.tar.xz file1 file2 | Create a compressed archive with xz (-J for xz compression) |
tar -xJf | tar -xJf archive.tar.xz | Extract an xz compressed archive (-J for xz compression) |
Advanced Options #
Command/Option | Example | Description |
---|---|---|
tar -A | tar -A -f archive.tar | Concatenate archives (-A option) |
tar -r | tar -r -f archive.tar file3 | Append files to an existing archive (-r option) |
tar -C | tar -cf archive.tar -C /path/to/dir file1 | Change directory before adding files (-C option) |
tar -v | tar -cvf archive.tar file1 | Verbose output showing files being processed (-v option) |
tar -p | tar -cpf archive.tar file1 | Preserve file permissions (-p option) |
tar --exclude | tar --exclude='*.tmp' -cf archive.tar * | Exclude files matching a pattern from the archive (--exclude option) |
tar --transform | tar --transform='s/old/new/' -cf archive.tar file1 | Rename files in the archive (--transform option) |
tar -zcvf | tar -zcvf archive.tar.gz file1 | Create a gzip compressed archive with verbose output (-z and -v options) |
This cheatsheet covers the essential BSD tar commands and options for managing archives.