Linux tar Command: Compress and Decompress tar.gz (xzf), and Extract a Single File
The Linux tar command bundles multiple files and folders into a single .tar file and extracts them again. A 40,960-byte .tar file made from a sample website folder became 3,907 bytes with -z compression and 3,200 bytes with -J compression, and after it was extracted into a folder set with -C, even execute permissions and symbolic links came back as they were.
The output below comes from a test folder on an AlmaLinux 9.8 server, run as root, with times in UTC. The web pages, logs and note file in the site folder are sample files made for the test. The IP addresses in the logs and the absolute path /root/work in 6️⃣ are also example values.
1️⃣ How to use the Linux tar command
The name tar is taken from the initial letters of tape archive (Tape ARchive). The test site folder holds two HTML files, deploy.sh with execute permission, a css folder, and a logs folder containing two logs and the symbolic link latest.log.
ls -lR sitesite:
total 12
-rw-r--r-- 1 root root 178 Oct 27 10:25 about.html
drwxr-xr-x 2 root root 23 Oct 26 16:40 css
-rwxr-xr-x 1 root root 90 Oct 26 17:05 deploy.sh
-rw-r--r-- 1 root root 232 Oct 27 10:20 index.html
drwxr-xr-x 2 root root 59 Oct 28 09:00 logs
site/css:
total 4
-rw-r--r-- 1 root root 1680 Oct 26 16:40 style.css
site/logs:
total 28
-rw-r--r-- 1 root root 22968 Oct 28 09:05 access.log
-rw-r--r-- 1 root root 510 Oct 28 13:15 error.log
lrwxrwxrwx 1 root root 10 Oct 28 09:00 latest.log -> access.log-c is the option for creating a new archive, and the name of the file to create goes after -f. Adding -v prints the names of the stored entries one after another.
tar -cvf backup/site.tar sitesite/
site/css/
site/css/style.css
site/logs/
site/logs/access.log
site/logs/error.log
site/logs/latest.log
site/index.html
site/about.html
site/deploy.shThe resulting site.tar is 40,960 bytes. That is larger than 25,658 bytes, the combined size of the six files inside; according to the official manual, tar adds a 512-byte header to each entry and fills the end of the file up to a multiple of 10,240 bytes. tar on its own does not make the size smaller; it combines the files into one.
ls -l backuptotal 40
-rw-r--r-- 1 root root 40960 Oct 29 01:08 site.tar-t prints only the list without extracting the archive. Adding -v as well puts the permissions, owner, size and modification time in front, and a symbolic link also shows the name of the file it is linked to, as in [-> access.log].
tar -tvf backup/site.tardrwxr-xr-x root/root 0 2026-10-27 10:25 site/
drwxr-xr-x root/root 0 2026-10-26 16:40 site/css/
-rw-r--r-- root/root 1680 2026-10-26 16:40 site/css/style.css
drwxr-xr-x root/root 0 2026-10-28 09:00 site/logs/
-rw-r--r-- root/root 22968 2026-10-28 09:05 site/logs/access.log
-rw-r--r-- root/root 510 2026-10-28 13:15 site/logs/error.log
lrwxrwxrwx root/root 0 2026-10-28 09:00 site/logs/latest.log -> access.log
-rw-r--r-- root/root 232 2026-10-27 10:20 site/index.html
-rw-r--r-- root/root 178 2026-10-27 10:25 site/about.html
-rwxr-xr-x root/root 90 2026-10-26 17:05 site/deploy.sh-x extracts the archive. If you put the folder to extract into after -C, the site folder is created as it is under the restore folder instead of the current folder.
tar -xvf backup/site.tar -C restoresite/
site/css/
site/css/style.css
site/logs/
site/logs/access.log
site/logs/error.log
site/logs/latest.log
site/index.html
site/about.html
site/deploy.shThe extracted deploy.sh kept its execute permission (rwx), and latest.log also came back linked to access.log — Symbolic links. The modification time of each file is also the same as before archiving.
ls -l restore/site restore/site/logsrestore/site:
total 12
-rw-r--r-- 1 root root 178 Oct 27 10:25 about.html
drwxr-xr-x 2 root root 23 Oct 26 16:40 css
-rwxr-xr-x 1 root root 90 Oct 26 17:05 deploy.sh
-rw-r--r-- 1 root root 232 Oct 27 10:20 index.html
drwxr-xr-x 2 root root 59 Oct 28 09:00 logs
restore/site/logs:
total 28
-rw-r--r-- 1 root root 22968 Oct 28 09:05 access.log
-rw-r--r-- 1 root root 510 Oct 28 13:15 error.log
lrwxrwxrwx 1 root root 10 Oct 28 09:00 latest.log -> access.log2️⃣ Compressing with tar
To compress while archiving, add -z (gzip) or -J (xz). A file made with -z gets .tar.gz at the end of its name, and the entries shown by -v are the same as for site.tar.
tar -czvf backup/site.tar.gz sitesite/
site/css/
site/css/style.css
site/logs/
site/logs/access.log
site/logs/error.log
site/logs/latest.log
site/index.html
site/about.html
site/deploy.shtar -cJf backup/site.tar.xz sitels -l backuptotal 48
-rw-r--r-- 1 root root 40960 Oct 29 01:08 site.tar
-rw-r--r-- 1 root root 3907 Oct 29 01:08 site.tar.gz
-rw-r--r-- 1 root root 3200 Oct 29 01:08 site.tar.xz| Archive | Option | Size | vs. site.tar |
|---|---|---|---|
| site.tar | -cf | 40,960 bytes | 100% |
| site.tar.gz | -czf | 3,907 bytes | 9.5% |
| site.tar.xz | -cJf | 3,200 bytes | 7.8% |
From the same site folder, xz came out 707 bytes smaller than gzip. A compressed archive is also listed with -t. Even when it was read with -tf and no compression option, tar recognized the compression method and printed the list.
tar -tf backup/site.tar.xzsite/
site/css/
site/css/style.css
site/logs/
site/logs/access.log
site/logs/error.log
site/logs/latest.log
site/index.html
site/about.html
site/deploy.shThe old-style syntax, written without the - in front of the options as in tar cfzp, also works as is. In this syntax the option letters take the following arguments in order, so f uses the first argument, backup/www.tgz, as the file name. -p is an option that preserves permissions when extracting, so the result was the same when archiving, and www.tgz was also 3,907 bytes, the same as site.tar.gz.
tar cfzp backup/www.tgz sitels -l backup/www.tgz-rw-r--r-- 1 root root 3907 Oct 29 01:08 backup/www.tgzWith -a, tar picks the compression method by looking at the end of the file name. A file given a name ending in .tar.xz was compressed with xz. Below is the result of checking the format of the two files with the file command.
tar -caf backup/site-a.tar.xz sitefile backup/site-a.tar.xz backup/www.tgzbackup/site-a.tar.xz: XZ compressed data
backup/www.tgz: gzip compressed data, from Unix, original size modulo 2^32 409603️⃣ Decompressing with tar
A tar.gz file is extracted by adding -z to -x. The folder given with -C must already exist, so the restore/gz folder was created first.
mkdir restore/gztar -xzvf backup/site.tar.gz -C restore/gzsite/
site/css/
site/css/style.css
site/logs/
site/logs/access.log
site/logs/error.log
site/logs/latest.log
site/index.html
site/about.html
site/deploy.shThe tar xzf often seen in search results is the old-style form of -xzf with only the leading - removed, and the options mean the same thing. Extracting www.tgz into the restore/old folder also produced the same site folder.
mkdir restore/oldtar xzf backup/www.tgz -C restore/oldls -l restore/old/sitetotal 12
-rw-r--r-- 1 root root 178 Oct 27 10:25 about.html
drwxr-xr-x 2 root root 23 Oct 26 16:40 css
-rwxr-xr-x 1 root root 90 Oct 26 17:05 deploy.sh
-rw-r--r-- 1 root root 232 Oct 27 10:20 index.html
drwxr-xr-x 2 root root 59 Oct 28 09:00 logsTo extract just one file, put site/css/style.css at the end, exactly as the name appears in the list (-t). The site/css folder that contains it is created as well.
mkdir restore/onetar -xzvf backup/site.tar.gz -C restore/one site/css/style.csssite/css/style.cssfind restore/onerestore/one
restore/one/site
restore/one/site/css
restore/one/site/css/style.cssIn this article's test environment (GNU tar 1.34), files were extracted even with a pattern containing * and no --wildcards. The official manual says wildcards are the default only for exclusion (--exclude), so when extracting by pattern, add --wildcards and wrap the pattern in single quotes. Only the three entries in the logs folder ending in .log (including the symbolic link) were extracted.
mkdir restore/logstar -xzvf backup/site.tar.gz -C restore/logs --wildcards 'site/logs/*.log'site/logs/access.log
site/logs/error.log
site/logs/latest.log--strip-components=1 removes the one folder at the very front of each name when extracting. The files inside were extracted directly into restore/web without the site folder, and the site/ line disappeared from the -v list.
mkdir restore/webtar -xzvf backup/site.tar.gz -C restore/web --strip-components=1site/css/
site/css/style.css
site/logs/
site/logs/access.log
site/logs/error.log
site/logs/latest.log
site/index.html
site/about.html
site/deploy.shls -l restore/webtotal 12
-rw-r--r-- 1 root root 178 Oct 27 10:25 about.html
drwxr-xr-x 2 root root 23 Oct 26 16:40 css
-rwxr-xr-x 1 root root 90 Oct 26 17:05 deploy.sh
-rw-r--r-- 1 root root 232 Oct 27 10:20 index.html
drwxr-xr-x 2 root root 59 Oct 28 09:00 logs4️⃣ tar command options
--exclude archives everything except the entries whose names match the pattern. When the logs are left out with the pattern *.log, the logs folder goes in as an empty folder, and the latest.log link, whose name also ends in .log, is left out too. The site-nolog.tar.gz made this way was 677 bytes.
tar -czvf backup/site-nolog.tar.gz --exclude='*.log' sitesite/
site/css/
site/css/style.css
site/logs/
site/index.html
site/about.html
site/deploy.sh-r adds files to the end of an existing .tar archive. After memo.txt is added, memo.txt appears in the last three lines of the list.
tar -rvf backup/site.tar memo.txtmemo.txttar -tf backup/site.tar | tail -3site/about.html
site/deploy.sh
memo.txtFiles cannot be added to a compressed archive with -r, so tar stops with [Cannot update compressed archives].
tar -rf backup/site.tar.gz memo.txttar: Cannot update compressed archives
tar: Error is not recoverable: exiting now-d compares the archive with the files in the current folder. When no file had changed, nothing was printed, and after one line was added to style.css and the comparison was run again, the result was lines saying that the modification time and size differ.
tar -df backup/site.tarecho changed >> site/css/style.csstar -df backup/site.tarsite/css/style.css: Mod time differs
site/css/style.css: Size differsThe options are gathered in the two tabs below.
| Option | Copy | Description |
|---|---|---|
| -c, --create | Create a new archive | |
| -x, --extract | Extract an archive | |
| -t, --list | List the contents without extracting | |
| -v, --verbose | Print the names of processed entries in order | |
| -f, --fileARCHIVE | Put the archive file name after it | |
| -z, --gzip | Compress with gzip | |
| -J, --xz | Compress with xz | |
| -j, --bzip2 | Compress with bzip2 | |
| -a, --auto-compress | Pick the compression method from the end of the file name | |
| -p, --preserve-permissions | Preserve permissions when extracting | |
| -C, --directoryDIR | Change to the given folder before archiving or extracting | |
| --wildcards | Extract only what matches a pattern | |
| --strip-componentsN | Strip the leading folder when extracting | |
| --excludePATTERN | Archive while leaving some items out | |
| -r, --append | Append to the end of an archive | |
| -u, --update | Append only newer files to the end | |
| -d, --diff | Compare with the current files | |
| -k, --keep-old-files | Extract without overwriting existing files | |
| -O, --to-stdout | View only the contents without extracting | |
| -T, --files-fromFILE | Archive only the names written in a list file |
| Option | Copy | Description |
|---|---|---|
| --delete | Delete the given entries from an archive | |
| -W, --verify | Read the files again after archiving to verify them | |
| --remove-files | Delete the original files from disk after archiving | |
| --skip-old-files | Skip existing files without a message when extracting | |
| --overwrite | Overwrite existing files when extracting | |
| -h, --dereference | Follow symbolic links and archive the files they point to | |
| -P, --absolute-names | Do not strip the leading / from names when archiving | |
| -N, --newerDATE | Archive only files newer than DATE | |
| --one-file-system | Do not cross into other file systems when archiving | |
| -X, --exclude-fromFILE | Exclude files matching the patterns written in FILE | |
| --exclude-vcs | Exclude version control system folders | |
| --transformEXPRESSION | Rename files with a sed substitution expression | |
| --numeric-owner | Always use numbers instead of user and group names | |
| --zstd | Compress with zstd |
🔲 tar command simulator
In the simulator below, you can type the steps yourself: archive and compress the site folder, then view the list or extract it into another folder.
5️⃣ Practical tar command examples
🔲 Backing up a web folder and checking the backup file
When moving a web folder to another server, archiving it without the leading site folder name lets you extract it straight into the new web root. The site folder in this section is a different test folder that only shares its name with the one in 1️⃣~4️⃣, so the file contents and sizes differ, and the modification times were set with touch. When -C was used to enter the site folder and the dot (.) was archived, every stored name started with ./.
tar -czvf backup/web.tar.gz -C site ../
./deploy.sh
./css/
./css/style.css
./about.html
./logs/
./logs/error.log
./logs/access.log
./logs/latest.log
./index.htmlWhen web.tar.gz, archived this way, was extracted into restore/web, which had been created in advance, the files and folders inside were placed there directly, without the site folder.
mkdir restore/webtar -xzf backup/web.tar.gz -C restore/webls -l restore/webtotal 20
-rw-r--r--. 1 root root 32 Oct 27 10:25 about.html
drwxr-xr-x. 2 root root 4096 Oct 26 16:40 css
-rwxr-xr-x. 1 root root 22 Oct 27 10:25 deploy.sh
-rw-r--r--. 1 root root 32 Oct 27 10:25 index.html
drwxr-xr-x. 2 root root 4096 Oct 28 09:00 logsTo keep only the logs separately, pipe the names selected with find to -T -. -T is the option that archives only the names written in a list file, and writing - in place of the file name makes it read the list passed through the pipe. When this was run, the -type f condition meant only the two regular files ending in .log were stored, and the symbolic link latest.log was left out.
find site/logs -name "*.log" -type f | tar -czvf backup/logs.tar.gz -T -site/logs/error.log
site/logs/access.logBefore the backup file was sent to another server, whether it could be read to the end was checked with the exit code. The intact web.tar.gz ended with 0 for both gzip -t and tar -tzf, while broken.tar.gz, which kept only the first 600 bytes, ended with 1 from gzip -t along with [unexpected end of file], and with 2 from tar -tzf along with [Unexpected EOF in archive].
head -c 600 backup/web.tar.gz > backup/broken.tar.gz; wc -c backup/web.tar.gz backup/broken.tar.gz1111 backup/web.tar.gz
600 backup/broken.tar.gz
1711 totalgzip -t backup/broken.tar.gz; echo $?
gzip: backup/broken.tar.gz: unexpected end of file
1tar -tzf backup/broken.tar.gz > /dev/null; echo $?
gzip: stdin: unexpected end of file
tar: Unexpected EOF in archive
tar: Error is not recoverable: exiting now
2🔲 Common Beginner Mistakes
If --exclude is written after the items to archive, the files meant to be left out go in anyway. When this was run, every file, .log files included, was stored, then the warning [--exclude '*.log' has no effect] appeared, and the exit code was 2.
tar -czvf backup/after.tar.gz site --exclude="*.log"site/
site/deploy.sh
site/css/
site/css/style.css
site/about.html
site/logs/
site/logs/error.log
site/logs/access.log
site/logs/latest.log
site/index.html
tar: The following options were used after any non-optional arguments in archive create or update mode. These options are positional and affect only arguments that follow them. Please, rearrange them properly.
tar: --exclude '*.log' has no effect
tar: Exiting with failure status due to previous errorsWhen the same option was moved in front of site, the logs folder was stored as an empty folder and the .log files were left out.
tar -czvf backup/before.tar.gz --exclude="*.log" sitesite/
site/deploy.sh
site/css/
site/css/style.css
site/about.html
site/logs/
site/index.htmlTo extract a single file, the name must be written exactly as it appears in the list. Trying to extract css/style.css from web.tar.gz, archived so that its names start with ./, gave [Not found in archive], and writing ./css/style.css extracted it.
mkdir restore/onetar -xzvf backup/web.tar.gz -C restore/one css/style.csstar: css/style.css: Not found in archive
tar: Exiting with failure status due to previous errorstar -xzvf backup/web.tar.gz -C restore/one ./css/style.css./css/style.css🔲 How to read tar results
-u is the option that appends to the end only the files that are newer than the archive. After site.tar was created, one line was added to style.css and its modification time was changed, and updating with -u appended only the one changed file, style.css.
tar -cf backup/site.tar sitetar -tvf backup/site.tar site/cssdrwxr-xr-x root/root 0 2026-10-26 16:40 site/css/
-rw-r--r-- root/root 15 2026-10-26 16:40 site/css/style.cssecho "p{color:#333}" >> site/css/style.css; touch -d "2026-11-02 14:30" site/css/style.csstar -uvf backup/site.tar sitesite/css/style.csstar -tvf backup/site.tar site/cssdrwxr-xr-x root/root 0 2026-10-26 16:40 site/css/
-rw-r--r-- root/root 15 2026-10-26 16:40 site/css/style.css
-rw-r--r-- root/root 29 2026-11-02 14:30 site/css/style.cssAfter the update, style.css appeared on two lines in the list. The first line still shows 15 bytes and 2026-10-26 16:40, while the newly added second line shows 29 bytes and 2026-11-02 14:30, and when the archive was extracted into another folder, the 29-byte content of the second line was what remained.
mkdir restore/utar -xf backup/site.tar -C restore/u; cat restore/u/site/css/style.cssbody{margin:0}
p{color:#333}The -C and --exclude options can be tried by typing them directly in the tar command simulator.
6️⃣ tar command errors
If the item to archive is given as an absolute path starting with /, [Removing leading `/' from member names] appears. Archiving still finishes, and the list shows it stored as root/work/site/, with the leading / removed.
tar -czf /root/work/backup/abs.tar.gz /root/work/sitetar: Removing leading `/' from member namestar -tzf /root/work/backup/abs.tar.gz | head -3root/work/site/
root/work/site/css/
root/work/site/css/style.cssExtracting a file that was not archived with tar ends with [This does not look like a tar archive].
tar -xf memo.txt -C restoretar: This does not look like a tar archive
tar: Exiting with failure status due to previous errorsIf -z is added but the file was not compressed with gzip, gzip reports [not in gzip format] and tar stops as well. This is the result of trying to extract the uncompressed site.tar with -xzf. This file is extracted with -xf, without a compression option.
tar -xzf backup/site.tar -C restore
gzip: stdin: not in gzip format
tar: Child returned status 1
tar: Error is not recoverable: exiting nowIf the item to archive does not exist, [Cannot stat: No such file or directory] is followed by a line saying that tar exited with a failure status. Even then, backup/x.tar.gz remained as a 45-byte file.
tar -czf backup/x.tar.gz nosuchtar: nosuch: Cannot stat: No such file or directory
tar: Exiting with failure status due to previous errorsIf the folder given with -C does not exist, tar stops right away with [Cannot open: No such file or directory].
tar -xzf backup/site.tar.gz -C restore/nonetar: restore/none: Cannot open: No such file or directory
tar: Error is not recoverable: exiting nowThe file name must come right after -f. If z is attached after f, as in -cfz, z becomes the file name and backup/bad.tar.gz is read as an item to archive, producing [Cannot stat]. A 40,960-byte file named z was created in the current folder.
tar -cfz backup/bad.tar.gz sitetar: backup/bad.tar.gz: Cannot stat: No such file or directory
tar: Exiting with failure status due to previous errorsls -l z-rw-r--r-- 1 root root 40960 Oct 29 01:08 zIf no item to archive is given at all, [Cowardly refusing to create an empty archive] appears.
tar -czf backup/empty.tar.gztar: Cowardly refusing to create an empty archive
Try 'tar --help' or 'tar --usage' for more information.The error output that came up while working with tar, and what to check next, are listed below.
| Output | Meaning | What to check next |
|---|---|---|
| Removing leading `/' from member names | The leading / of an absolute path is removed when storing (archiving continues) | Whether the full path is created under the -C folder when extracting |
| This does not look like a tar archive | Not a file archived with tar | The actual format with the file command |
| gzip: stdin: not in gzip format | -z was added, but the file was not compressed with gzip | -xf without the compression option |
| Cannot stat: No such file or directory | The item to archive does not exist | The -C base folder and the item name |
| Cannot open: No such file or directory | The -C folder or the folder of the -f file does not exist | Create the folder first with mkdir |
| Cowardly refusing to create an empty archive | No item to archive was given | The items after the file name that follows -f |
| Cannot update compressed archives | Tried to add to a compressed archive with -r | Archive as .tar, add, then compress |
🔢 FAQ & Recommended Content
Scheduling
Linux: crontab Setup
The crontab setup for running commands automatically at set times. Use it to schedule a job that creates a backup file every night.
Disk
du command: checking file disk usage
The du command for checking how much disk space files and directories use. Use it to see the size of a folder before archiving.
Search
Linux find Command: The -name, -type, -size, -mtime Options and -delete
The find command for finding files by conditions such as name, size and modification time. It is used alongside tar to pick out old backup files.
ℹ️ Affiliate Disclosure
This site's content contains affiliate links. When a visitor buys a product or service through one of them, the site receives a commission from the seller. The amount the buyer pays(it goes down during event discounts ↓)does not go up. Posted prices, discounts, and stock reflect the time of writing and may differ, so confirm with the seller before buying. Products are chosen and reviewed by our own standards, and commissions do not affect the order or content of reviews.