TAR - Compress and Extract
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
tar {-c|-x|-t} [options] -f archive_file [files_or_folders]
The main options are as follows.
| Option | Description |
|---|---|
-c | Create an archive (create) |
-x | Extract an archive (extract) |
-t | List the contents of an archive (list) |
-f | Specify the archive file to work with (file) |
-a | Choose the compression format from the file extension (create only) |
-v | Display the names of the files processed (verbose) |
-C | Switch the working folder (used for the extraction and source folders) |
-z | Compress with gzip (.tar.gz). Not needed when extracting |
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.
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
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

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

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

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

知識・才能ゼロでもらく~に月10万円稼ぐ! よくわかるAI副業超入門
Extract a ZIP File
Use -x to extract.
tar -x -f backup.zip
Specify the Extraction Folder
Use -C to specify the folder to extract into.
tar -x -f backup.zip -C D:\Work\restore
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
if not exist D:\Work\restore mkdir D:\Work\restore
tar -x -f backup.zip -C D:\Work\restore
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
When there are many files, pipe the output to
or to make it easier to read. For details on pipes, see .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.
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.
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.
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.
tar -a -c -f backup.tar.gz docs
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.@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
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.
| Item | TAR | Compress-Archive |
|---|---|---|
| Where to run | Command Prompt and PowerShell | PowerShell |
| Supported formats | zip, tar, tar.gz, and more | zip only |
| Length of syntax | Short | Longer |
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
If a version number is displayed, you can use it (the number shown varies by environment).
Summary
| Task | Command |
|---|---|
| Compress a folder to ZIP | tar -a -c -f output.zip folder |
| Extract a ZIP | tar -x -f input.zip |
| Specify the extraction folder | tar -x -f input.zip -C destination |
| List the contents | tar -t -f input.zip |
| Exclude files | --exclude "pattern" |
| Compress as tar.gz | tar -a -c -f output.tar.gz folder |