TAR - Compress and Extract

Maintained on

You can zip a folder or extract a downloaded ZIP file from the right-click menu in File Explorer. But when you want to automate backups in a batch file, it is handy to do the same thing from a command.

Windows 10 (version 1803) and later include the TAR command out of the box. You can create and extract ZIP files without installing any extra software.

TAR Command Basics

Syntax
tar {-c|-x|-t} [options] -f archive_file [files_or_folders]

The main options are as follows.

OptionDescription
-cCreate an archive (create)
-xExtract an archive (extract)
-tList the contents of an archive (list)
-fSpecify the archive file to work with (file)
-aChoose the compression format from the file extension (create only)
-vDisplay the names of the files processed (verbose)
-CSwitch the working folder (used for the extraction and source folders)
-zCompress with gzip (.tar.gz). Not needed when extracting
The "-" before options

In Windows TAR, options are written with a leading -, as in tar -cf. You can combine several options, such as -cvf. Because -f takes the file name that follows it, put it last when combining options.

Compress a Folder into a ZIP File

With the -a option, the compression format is chosen from the extension of the output file. Give it a .zip extension to create a ZIP file.

Compress
tar -a -c -f backup.zip docs
- □ ×
Command Prompt Icon
Command Prompt
Microsoft Windows [Version xx.x.xxxxx.xxx]
(c) 2026 Ribbit App Development All rights reserved.
 
C:\users\user>tar -a -c -f backup.zip docs

The docs folder in the current directory is compressed into backup.zip. For details on the current directory, see

.

Without -a, you do not get a ZIP

If you run tar -c -f backup.zip docs without -a, the result is an uncompressed tar file even though it has a .zip extension. Always add -a when you want a real ZIP file.

Bundle Multiple Files

You can specify several files and folders separated by spaces.

tar -a -c -f report.zip report.txt data.csv images

Watch the File Names as They Are Added

With -v, the names of the files added to the archive are displayed one by one.

tar -a -c -v -f backup.zip docs
- □ ×
Command Prompt Icon
Command Prompt
Microsoft Windows [Version xx.x.xxxxx.xxx]
(c) 2026 Ribbit App Development All rights reserved.
 
C:\users\user>tar -a -c -v -f backup.zip docs
a docs
a docs/readme.txt
a docs/memo.txt
C:\users\user>
おすすめ書籍※ 広告を含む場合があります
中小企業経営者のためのRPA入門 RPA導入を成功させる方法

中小企業経営者のためのRPA入門 RPA導入を成功させる方法

60分でわかる! AIエージェント 超入門

60分でわかる! AIエージェント 超入門

コマンドラインの黒い画面が怖いんです。

コマンドラインの黒い画面が怖いんです。

知識・才能ゼロでもらく~に月10万円稼ぐ! よくわかるAI副業超入門

知識・才能ゼロでもらく~に月10万円稼ぐ! よくわかるAI副業超入門

Extract a ZIP File

Use -x to extract.

Extract
tar -x -f backup.zip
- □ ×
Command Prompt Icon
Command Prompt
Microsoft Windows [Version xx.x.xxxxx.xxx]
(c) 2026 Ribbit App Development All rights reserved.
 
C:\users\user>tar -x -f backup.zip

Specify the Extraction Folder

Use -C to specify the folder to extract into.

Extract
tar -x -f backup.zip -C D:\Work\restore
Create the destination folder first

If the folder specified with -C does not exist, TAR does not create it and fails with an error. Create it beforehand, as described in

.

Create
if not exist D:\Work\restore mkdir D:\Work\restore
tar -x -f backup.zip -C D:\Work\restore
Files with the same name are overwritten

If the destination already contains a file with the same name, it is overwritten without confirmation. When extracting into a folder that holds important files, we recommend extracting into a separate folder first and checking the contents. If you do not want to overwrite files, add the -k option to keep existing files.

Extract Only Specific Files

Specify a path inside the archive at the end to extract only that file. Check the archive contents first, then specify the path.

tar -x -f backup.zip docs/readme.txt

Check the Contents of an Archive

To check the contents before extracting, use -t.

tar -t -f backup.zip
- □ ×
Command Prompt Icon
Command Prompt
Microsoft Windows [Version xx.x.xxxxx.xxx]
(c) 2026 Ribbit App Development All rights reserved.
 
C:\users\user>tar -t -f backup.zip
docs/
docs/readme.txt
docs/memo.txt
C:\users\user>
List
tar -t -f backup.zip | find ".txt"

Exclude Files from the Archive

Use --exclude to leave files that match a pattern out of the archive. Specify the pattern right after --exclude; wildcards such as * are supported.

Compress
tar -a -c -f backup.zip --exclude "*.tmp" --exclude "*.log" docs

By convention, write --exclude before the name of the folder to compress. The pattern is interpreted by TAR rather than by Command Prompt, so enclose it in quotation marks, as in "*.tmp".

Compress Files in Another Folder

When you use -C while compressing, TAR switches to the specified folder and then adds the files and folders that follow. This keeps extra folder levels such as C:\Users\... out of the archive. Write -C before the files or folders it applies to.

Compress
tar -a -c -f D:\Backup\docs.zip -C C:\Users\user\Documents docs

The structure inside the archive starts with docs/.... The folder specified with -C, C:\Users\user\Documents, is not included in the archive itself.

Keep the archive out of the folder you are compressing

If you create the output ZIP file inside the folder you are compressing, the archive being created ends up among the files to add, which can cause errors or unexpected results. As in the example above, specify an output location outside the folder you are compressing.

Work with tar.gz Files

The .tar.gz format is commonly used when exchanging files with Linux and other environments. With -a, gzip compression is chosen automatically from the .tar.gz extension.

Create
tar -a -c -f backup.tar.gz docs
Extract
tar -x -f backup.tar.gz

The format is detected automatically when extracting, so you do not need -z.

Create a Backup with a Date in the Name

Combine this with the date techniques introduced in

to create backups whose names include the date.

backup.bat
@echo off
setlocal

set "stamp=%date:~0,4%%date:~5,2%%date:~8,2%"
tar -a -c -f "D:\Backup\docs_%stamp%.zip" -C "C:\Users\user\Documents" docs

if %errorlevel% neq 0 (
  echo The backup failed.
  exit /b 1
)
echo The backup is complete.

The format of %date% depends on your regional settings. The example above assumes the 2026/10/26 format. To make it work reliably, see the methods in

. For details on exit codes, see .

Differences from PowerShell’s Compress-Archive

PowerShell also has the Compress-Archive and Expand-Archive cmdlets for ZIP files. You can call PowerShell from Command Prompt, but it takes a little longer to start and the syntax is longer.

ItemTARCompress-Archive
Where to runCommand Prompt and PowerShellPowerShell
Supported formatszip, tar, tar.gz, and morezip only
Length of syntaxShortLonger

For simple compression and extraction, TAR is convenient because you can run it directly from Command Prompt.

Not Available on Older Versions of Windows

The TAR command is included in Windows 10 version 1803 (April 2018) and later. On older systems, you will see an error like 'tar' is not recognized as an internal or external command, operable program or batch file.

You can check whether TAR is available with the following command.

tar --version
- □ ×
Command Prompt Icon
Command Prompt
Microsoft Windows [Version xx.x.xxxxx.xxx]
(c) 2026 Ribbit App Development All rights reserved.
 
C:\users\user>tar --version
bsdtar 3.7.7 - libarchive 3.7.7 zlib/1.3.1 liblzma/5.4.3 bz2lib/1.0.8
C:\users\user>

If a version number is displayed, you can use it (the number shown varies by environment).

Summary

TaskCommand
Compress a folder to ZIPtar -a -c -f output.zip folder
Extract a ZIPtar -x -f input.zip
Specify the extraction foldertar -x -f input.zip -C destination
List the contentstar -t -f input.zip
Exclude files--exclude "pattern"
Compress as tar.gztar -a -c -f output.tar.gz folder

Practice Questions

Practice Problem

Which command compresses the docs folder into a ZIP file named backup.zip?

回答がサーバーに送信されることはありません
Practice Problem

What happens when you run tar -x -f backup.zip -C D:\Work\restore and D:\Work\restore does not exist?

回答がサーバーに送信されることはありません
#Command Prompt #TAR #ZIP #Compression #Extraction #Command