Daily IT Tip
Learn something new every day. One tip, one concept, one command at a time.
Linux Command: tar
Archive files using tape archive format
tar (tape archive) creates and extracts archive files. While tar itself does not compress, it is almost always combined with compression: tar.gz (gzip), tar.bz2 (bzip2), tar.xz (xz). tar is the standard archive format for Linux backup and distribution. tar preserves file permissions, ownership, timestamps, symbolic links, and directory structure. It is the preferred format for source code distribution, system backups, and data transfer. The most common commands are tar czf (create gzipped archive) and tar xzf (extract gzipped archive). Modern tar auto-detects compression format with tar xf.
Syntax
tar [OPTION]... [FILE]...Key Options
-cโ Create archive-xโ Extract archive-zโ Compress/decompress with gzip-jโ Compress with bzip2-Jโ Compress with xz-fโ Specify archive filename
Examples
Create gzipped archive
tar czf backup.tar.gz /home/user/Extract archive
tar xf archive.tar.gzExtract to specific directory
tar xf release.tar.gz -C /opt/Pro Tips
- tar czf = create gzip file. tar xf = extract file. tar tf = list (tell) file. f always comes last before filename.
- Use tar tf archive first to check contents. Some archives extract files without a top-level directory, cluttering your current directory.
- Speed: gzip > bzip2 > xz. Compression ratio: xz > bzip2 > gzip. gzip is the default for most uses. xz for distribution.
Learn more: Full tar reference โ
Share this tip
Get Daily IT Tips in Your Inbox
Join our newsletter and receive a new IT tip every day โ Linux commands, DevOps tricks, security best practices, and more. Free, no spam, unsubscribe anytime.