tar Linux Command
Archives multiple files into a single tarball. Use -czf to compress with gzip.
What does tar do?
Archives multiple files into a single tarball. Use -czf to compress with gzip.
Syntax
tar [options] archive_name filesPractical example
tar -czf backup.tar.gz /home/user/dataQuestions about tar
How do I create a tar.gz archive?
Use `tar -czf archive.tar.gz /path/to/files` to create a gzip-compressed tar archive. The `-c` creates, `-z` uses gzip, `-f` specifies the filename.
How do I extract a tar.gz file?
Use `tar -xzf archive.tar.gz` to extract. The `-x` extracts, `-z` handles gzip, `-f` specifies the file. Add `-C /path` to extract to a directory.
How do I extract a tar file to a specific directory?
Use `tar -xf archive.tar -C /destination/` to extract files to the specified directory instead of the current one.
How do I list contents of a tar archive?
Use `tar -tf archive.tar` to list all files in the archive without extracting. Add `-v` for verbose listing with sizes and dates.
How do I create a tar.bz2 archive?
Use `tar -cjf archive.tar.bz2 /path/to/files` to create a bzip2-compressed archive. Bzip2 has better compression but is slower than gzip.
How do I extract a tar.xz file?
Use `tar -xJf archive.tar.xz` to extract xz-compressed archives. The `-J` flag handles xz compression.
How do I exclude files from a tar archive?
Use `tar -czf archive.tar.gz --exclude='*.log' /path/` to exclude files matching a pattern.
How do I create a tar of only specific file types?
Use `tar -czf archive.tar.gz /path/*.txt` to include only .txt files, or combine with `find` for complex selections.
Related Compression Commands
7zHigh-compression archiver supporting many formats (7z, zip, rar, tar, etc.).
gzipCompresses a single file using the gzip algorithm (replaces original with .gz).
unxzDecompresses a .xz file back to its original form.
gunzipDecompresses a .gz file back to its original form.
xzCompresses files using the xz (LZMA2) algorithm — very high compression ratio.
bzip2Compresses files using the bzip2 algorithm (better compression than gzip, slower).