Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

1.11 Compression and Decompression

tar — Tape Archive Utility

Format:

tar [options] archive_name [files_or_directories]

The typical structural arrangement is:

tar -options archive_file targets_to_archive

Common Options:

  • -c: Create a new archive.

  • -x: Extract files from an archive.

  • -t: List the contents of an archive (table of contents).

  • -r: Append files to the end of an archive.

  • -u: Only append files that are newer than the copy in the archive.

  • -f: Use archive file (mandatory). The -f option must be followed immediately by the archive file name.

OptionCompression MethodStandard Extension
-zgzip.tar.gz
-jbzip2.tar.bz2
-Jxz.tar.xz
-aAuto-detectbased on Automatic extension

Examples:

$ tar -cvf archive.tar dir/ # Create a tar archive (uncompressed)

$ tar -czvf archive.tar.gz dir/ # Create a gzip compressed tar archive

$ tar -cJvf archive.tar.xz dir/ # Create an xz compressed tar archive (high compression ratio)

$ tar -xvf archive.tar # Extract an uncompressed tar archive

$ tar -xzvf archive.tar.gz # Extract a gzip compressed tar archive

$ tar -xzvf archive.tar.gz -C /target/path # Extract to a designated target path

$ tar -tvf archive.tar # List archive contents verbosely

Common Control Options:

OptionArchitectural Meaning / Function
-vVerbose mode (display file progression details).
-CChange to specified directory before、executing operations.
--excludeExclude files matching a specified pattern.
-pPreserve original file permissions and
--strip-components=nStrip n leading components from file names on extraction.

Advanced Practical Invocations:

$ tar -czvf backup.tar.gz /project --exclude="*.log" # Exclude file pattern example

$ tar -xvf archive.tar file.txt # Extract a specific file from archive

$ tar -czvf backup.tar.gz . # Archive the current working directory

Standard Archiving and Extraction Examples:

$ tar -xvf file.tar

$ tar -czvf file.tar.gz dir

$ tar -xzvf file.tar.gz

$ zip -r file.zip dir

$ unzip file.zip
Copyright © 2026 Guangzhou eSmart Information Technology Co., Ltd. All Rights Reserved.
All content in this book is copyrighted by the author. For personal learning and exchange only. Commercial use without authorization is strictly prohibited. Email:

✉ 3348194517@qq.com | hai616878@gmail.com