Linux tar Command: Compress and Decompress tar.gz (xzf), and Extract a Single File

Test environmentRHEL familyAlmaLinux 9.8root account
Compression · archive commandstarLocation/usr/bin/tar

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

  • ↪️ Archiving (-c), listing (-t) and extracting (-x)

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 site
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

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 site
site/
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.sh

The 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 backup
total 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.tar
drwxr-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 restore
site/
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.sh

The 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/logs
restore/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.log

2️⃣ Compressing with tar

  • ↪️ tar.gz (-z) and tar.xz (-J), size comparison

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 site
site/
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.sh
tar -cJf backup/site.tar.xz site
ls -l backup
total 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
ArchiveOptionSizevs. site.tar
site.tar-cf40,960 bytes100%
site.tar.gz-czf3,907 bytes9.5%
site.tar.xz-cJf3,200 bytes7.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.xz
site/
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.sh

The 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 site
ls -l backup/www.tgz
-rw-r--r-- 1 root root 3907 Oct 29 01:08 backup/www.tgz

With -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 site
file backup/site-a.tar.xz backup/www.tgz
backup/site-a.tar.xz: XZ compressed data
backup/www.tgz:       gzip compressed data, from Unix, original size modulo 2^32 40960

3️⃣ Decompressing with tar

  • ↪️ tar xzf and the extraction folder (-C), extracting a single file

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/gz
tar -xzvf backup/site.tar.gz -C restore/gz
site/
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.sh

The 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/old
tar xzf backup/www.tgz -C restore/old
ls -l restore/old/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

To 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/one
tar -xzvf backup/site.tar.gz -C restore/one site/css/style.css
site/css/style.css
find restore/one
restore/one
restore/one/site
restore/one/site/css
restore/one/site/css/style.css

In 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/logs
tar -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/web
tar -xzvf backup/site.tar.gz -C restore/web --strip-components=1
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.sh
ls -l restore/web
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

4️⃣ tar command options

  • ↪️ --exclude, adding with -r, comparing with -d

--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' site
site/
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.txt
memo.txt
tar -tf backup/site.tar | tail -3
site/about.html
site/deploy.sh
memo.txt

Files cannot be added to a compressed archive with -r, so tar stops with [Cannot update compressed archives].

tar -rf backup/site.tar.gz memo.txt
tar: 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.tar
echo changed >> site/css/style.css
tar -df backup/site.tar
site/css/style.css: Mod time differs
site/css/style.css: Size differs

The options are gathered in the two tabs below.

OptionCopyDescription
-c, --createCreate a new archive
-x, --extractExtract an archive
-t, --listList the contents without extracting
-v, --verbosePrint the names of processed entries in order
-f, --fileARCHIVEPut the archive file name after it
-z, --gzipCompress with gzip
-J, --xzCompress with xz
-j, --bzip2Compress with bzip2
-a, --auto-compressPick the compression method from the end of the file name
-p, --preserve-permissionsPreserve permissions when extracting
-C, --directoryDIRChange to the given folder before archiving or extracting
--wildcardsExtract only what matches a pattern
--strip-componentsNStrip the leading folder when extracting
--excludePATTERNArchive while leaving some items out
-r, --appendAppend to the end of an archive
-u, --updateAppend only newer files to the end
-d, --diffCompare with the current files
-k, --keep-old-filesExtract without overwriting existing files
-O, --to-stdoutView only the contents without extracting
-T, --files-fromFILEArchive only the names written in a list file
OptionCopyDescription
--deleteDelete the given entries from an archive
-W, --verifyRead the files again after archiving to verify them
--remove-filesDelete the original files from disk after archiving
--skip-old-filesSkip existing files without a message when extracting
--overwriteOverwrite existing files when extracting
-h, --dereferenceFollow symbolic links and archive the files they point to
-P, --absolute-namesDo not strip the leading / from names when archiving
-N, --newerDATEArchive only files newer than DATE
--one-file-systemDo not cross into other file systems when archiving
-X, --exclude-fromFILEExclude files matching the patterns written in FILE
--exclude-vcsExclude version control system folders
--transformEXPRESSIONRename files with a sed substitution expression
--numeric-ownerAlways use numbers instead of user and group names
--zstdCompress 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.

tar command simulatorGNU tar 1.34
[root@server work]#
It runs only in your browser and does not actually archive or compress any files. The size of the first archive of the site folder is a measured value, and the sizes of compressed files newly created in the simulator are estimated values. Use ↑↓ to recall earlier commands.

5️⃣ Practical tar command examples

  • ↪️ Backing up a web folder and checking the backup file, common beginner mistakes

🔲 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.html

When 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/web
tar -xzf backup/web.tar.gz -C restore/web
ls -l restore/web
total 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 logs

To 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.log

Before 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.gz
1111 backup/web.tar.gz
 600 backup/broken.tar.gz
1711 total
gzip -t backup/broken.tar.gz; echo $?

gzip: backup/broken.tar.gz: unexpected end of file
1
tar -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 errors

When 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" site
site/
site/deploy.sh
site/css/
site/css/style.css
site/about.html
site/logs/
site/index.html

To 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/one
tar -xzvf backup/web.tar.gz -C restore/one css/style.css
tar: css/style.css: Not found in archive
tar: Exiting with failure status due to previous errors
tar -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 site
tar -tvf backup/site.tar site/css
drwxr-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
echo "p{color:#333}" >> site/css/style.css; touch -d "2026-11-02 14:30" site/css/style.css
tar -uvf backup/site.tar site
site/css/style.css
tar -tvf backup/site.tar site/css
drwxr-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.css

After 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/u
tar -xf backup/site.tar -C restore/u; cat restore/u/site/css/style.css
body{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

  • ↪️ The Removing leading notice and the not in gzip format error

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/site
tar: Removing leading `/' from member names
tar -tzf /root/work/backup/abs.tar.gz | head -3
root/work/site/
root/work/site/css/
root/work/site/css/style.css

Extracting a file that was not archived with tar ends with [This does not look like a tar archive].

tar -xf memo.txt -C restore
tar: This does not look like a tar archive
tar: Exiting with failure status due to previous errors

If -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 now

If 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 nosuch
tar: nosuch: Cannot stat: No such file or directory
tar: Exiting with failure status due to previous errors

If 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/none
tar: restore/none: Cannot open: No such file or directory
tar: Error is not recoverable: exiting now

The 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 site
tar: backup/bad.tar.gz: Cannot stat: No such file or directory
tar: Exiting with failure status due to previous errors
ls -l z
-rw-r--r-- 1 root root 40960 Oct 29 01:08 z

If no item to archive is given at all, [Cowardly refusing to create an empty archive] appears.

tar -czf backup/empty.tar.gz
tar: 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.

OutputMeaningWhat to check next
Removing leading `/' from member namesThe 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 archiveNot a file archived with tarThe 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 directoryThe item to archive does not existThe -C base folder and the item name
Cannot open: No such file or directoryThe -C folder or the folder of the -f file does not existCreate the folder first with mkdir
Cowardly refusing to create an empty archiveNo item to archive was givenThe items after the file name that follows -f
Cannot update compressed archivesTried to add to a compressed archive with -rArchive as .tar, add, then compress

🔢 FAQ & Recommended Content

Archiving with tar -cjf on a server without the bzip2 program installed produced [bzip2: command not found] and [Child returned status 127], and backup/site.tar.bz2 was left as a 0-byte file. -j calls the bzip2 program to compress, so install the bzip2 package and archive again, or compress with -z or -J.

After the content of restore/gz/site/about.html, extracted earlier, was changed to edited and the archive was extracted again to the same place, tar overwrote it with the original content without asking. Extracting with -k prints [Cannot open: File exists] for each file that already exists, and that file is not overwritten.

-O sends the contents to standard output without creating files. Reading site/logs/error.log inside site.tar.gz with tar -xzOf and passing it to head -3 shows the first three lines of the log.

ℹ️ 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.

Leave a Reply

Your email address will not be published. Required fields are marked *

Prove your humanity: 10   +   2   =