Language
日本語
English

Caution

JavaScript is disabled in your browser.
This site uses JavaScript for features such as search.
For the best experience, please enable JavaScript before browsing this site.

  1. Home
  2. Bash Dictionary
  3. tar

tar

tar is a command for bundling files and directories into a single archive file. It can also compress archives when combined with gzip or bzip2. It is the most widely used archiving tool on Linux and macOS.

Syntax

Create an archive (with gzip compression).

tar -czf archive-name.tar.gz file-or-directory

Extract an archive.

tar -xzf archive-name.tar.gz

List archive contents (without extracting).

tar -tzf archive-name.tar.gz

Create an archive (with bzip2 compression).

tar -cjf archive-name.tar.bz2 file-or-directory

Extract to a specific directory.

tar -xzf archive-name.tar.gz -C destination-directory

Options

OptionDescription
-cCreates an archive (create).
-xExtracts an archive (extract).
-tLists the contents of an archive (list).
-zCompresses or decompresses with gzip (.tar.gz / .tgz).
-jCompresses or decompresses with bzip2 (.tar.bz2).
-JCompresses or decompresses with xz (.tar.xz).
-f filenameSpecifies the archive filename (required).
-vShows each file as it is processed (verbose).
-C directorySpecifies the directory to extract into.
--exclude=patternExcludes matching files from the archive.
-pPreserves file permissions and ownership.
--strip-components=NStrips the leading N path components when extracting.

Sample Code

The examples below use the following directory structure.

📁 ~/project/ 📁 src/ 📄 index.php 📁 css/ 📄 style.css 📁 docs/ 📄 README.md 📁 node_modules/

Create a gzip-compressed archive of the directory. Adding -v prints each file as it is processed.

tar -czvf project.tar.gz ~/project/
~/project/
~/project/src/
~/project/src/index.php
~/project/src/css/
~/project/src/css/style.css
~/project/docs/
~/project/docs/README.md

List the contents of an archive without extracting it.

tar -tzf project.tar.gz
~/project/
~/project/src/
~/project/src/index.php
~/project/src/css/
~/project/src/css/style.css
~/project/docs/
~/project/docs/README.md

Extract the archive into the current directory.

tar -xzf project.tar.gz

Extract the archive into a specific directory.

tar -xzf project.tar.gz -C /tmp/restore/

Create an archive while excluding .git and node_modules.

archive_clean.sh
tar -czf project-clean.tar.gz \
    --exclude='.git' \
    --exclude='node_modules' \
    ~/project/
bash archive_clean.sh

Create a date-stamped backup.

backup.sh
today=$(date +%Y%m%d)
tar -czf "backup_${today}.tar.gz" ~/project/
bash backup.sh

Create an archive with bzip2 compression (higher compression ratio than gzip, but slower).

tar -cjf project.tar.bz2 ~/project/

Check the compressed file size of the archive.

ls -lh project.tar.gz
-rw-r--r-- 1 user user 2.3M Mar  6 12:00 project.tar.gz

Notes

The most commonly used option combinations are -czf (Create + gZip + File) and -xzf (eXtract + gZip + File). Adding -v prints each file being processed so you can track progress.

In backup scripts, it is important to use --exclude to omit unnecessary directories such as .git and node_modules. For other commands that work with compressed files, see gzip / bzip2 / zip.

If you find any errors or copyright issues, please .