GUN tar can be used to create and manipulate archive files.
By default, GNU tar handle archive files in gnu or posix format (usually with suffix .tar). However, it can work with various compression programs (gzip, bzip2, lzip, lzma, lzop, zstd, xz, compress).
Suffix | Compression Program |
.xz | xz |
.gz | gzip |
.tgz | gzip |
.taz | gzip |
.bz2 | bzip2 |
.tz2 | bzip2 |
.tbz2 | bzip2 |
.tbz | bzip2 |
.lzma | lzma |
.tlz | lzma |
.lzo | lzop |
.lz | lzip |
.zst | zstd |
.tzst | zstd |
.Z | compress |
.taZ | compress |
While compressing or decompressing archive files, if getting error “xxx command not found” or “cannot run xxx”, you will need to install corresponding compression program.
GNU tar has three option styles, which are traditional style, Unix (short-option) style and GNU (long-option) style, here only use Unix style.
Basic usage
To list all contents of an archive file:
tar -tvf archive.tar
To extract all files from an archive file to current path:
tar -xvf archive.tar
To extract all files from an archive file to a specific path:
tar -xvf archive.tar -C /path/to/somewhere
To archive multiple files or directories into one tar file:
tar -cvf archive.tar somefile somedir
To archive multiple files or directories into one gzip file:
tar -cvzf archive.tar.gz somefile somedir
To archive multiple files or directories into one bzip2 file:
tar -cvjf archive.tar.bz2 somefile somedir
To archive multiple files or directories into one xz file:
tar -cvJf archive.tar.xz somefile somedir
To archive multiple files or directories into one file, whose format depends on suffix of the specified file name:
tar -cvaf archive.tar.bz2 somefile somedir
Advanced usage
To extract specific files from an archive file to current path:
tar -xvf archive.tar filename1 filename2
To extract files with specific pattern:
tar -xvf archive.tar --wildcards '*.txt'
To extract files excluded by specific pattern:
tar -xvf archive.tar --exclude '*.txt'
Change the owner of extracted files to current user:
tar -xvf archive.tar --no-same-owner
To append files to a tar file:
tar -rvf archive.tar somefile somedir
To append updated files to a tar file (no overwrite the old file):
tar -uvf archive.tar somefile somedir
To delete files from a tar file:
tar -vf archive.tar --delete somefile somedir
Unfortunately, for archive file that was created by third-party compression program, you can not append files to it, update it, or delete files from it.
Learn more
To show help of all available commands and options:
tar --usage
To show help of a specific command:
tar --help
To learn more from manual:
man tar