GNU Tar Cheatsheet #
GNU tar is a widely used version with extensive features and options.
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 -cjf | tar -cjf 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 -czf | tar -czf 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 --append | tar --append -f archive.tar file3 | Append files to an existing archive (--append option) |
tar --delete | tar --delete -f archive.tar file1 | Remove files from an archive (--delete 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 GNU tar commands and options for managing archives.