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
| Option | Description |
|---|---|
| -c | Creates an archive (create). |
| -x | Extracts an archive (extract). |
| -t | Lists the contents of an archive (list). |
| -z | Compresses or decompresses with gzip (.tar.gz / .tgz). |
| -j | Compresses or decompresses with bzip2 (.tar.bz2). |
| -J | Compresses or decompresses with xz (.tar.xz). |
| -f filename | Specifies the archive filename (required). |
| -v | Shows each file as it is processed (verbose). |
| -C directory | Specifies the directory to extract into. |
| --exclude=pattern | Excludes matching files from the archive. |
| -p | Preserves file permissions and ownership. |
| --strip-components=N | Strips the leading N path components when extracting. |
Sample Code
The examples below use the following directory structure.
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 contact us.