ZIP Up Files from the Command Line
One of the most useful tools in any computer users toolbox is an archiver/compression tool. Examples include 7-Zip, WinZip, PKZip, and WinRAR for the Windows platform, and gzip/tar for the Linux platform. Most people are familiar with the graphical user interfaces that these tools provide, dragging and dropping files into, and out of archives. But did you know that all of these tools also have commandline versions available? These are great for using in batch files to automate the process of compressing, or uncompressing, files. This makes it possible, with one click of a mouse button, to do things like:
- Make a quick backup for safekeeping of the 20 most critical files and folders, no matter that they are scattered across two hard drives in a dozen different locations.
- Copy the files for any current projects onto a USB drive, to take home for the weekend, or take on the road.
- “Snapshot” a set of files, freezing them in time — just in case it is ever necessary to revert them back to the way they were.
- Archive old files before deleting them, as a simple precaution.
(Not) Automating Extractions: As you can see, most of the cases that call for automation are ones where original files are being compressed and stored into an archive, rather than files being extracted from an archive. For file extraction, it’s typically easiest to use the GUI, to double-click on the archive file and then click-and-drag the desired files out of the archive. So, the examples below will focus on the former, but don’t hesitate to post a comment should you need to see examples of latter.
7-Zip: For this example, I will use 7-Zip, a free open source archiver for the Windows platform (see Quick Link: 7-Zip 4.47 in Beta), but the concepts are the same no matter which tool you prefer. Only the details of syntax will differ.
When you run the 7-Zip installer, it actually installs several versions of itself as separate executables. The executable that all of the shortcuts link to is 7zFM.exe, known as the “7-Zip File Manager.” That’s the one that presents a graphical user interface. There are also a couple of commandline versions of the executable:
7z.exe — This version of the commandline program is fully featured, because it utilizes all of the plug-in modules that are included with the 7-Zip package. For example, if you want to be able to unarchive a RAR file, then you will need to use this version. Also, if you want to create an archive that is self-extracting, then you’ll need to use this version.
7za.exe — This is the standalone version of the commandline program. It only supports certain built-in compression formats (7z, zip, gzip, bzip2, Z and tar). 7za.exe doesn’t depend on any other files besides the EXE itself. So, this version is particularly handy for carrying around on a USB drive, or for any other need where it is nice to only have to worry about the one EXE file being in place.
C: CD books 7z.exe u -tzip classics.zip classics -r
The general syntax for calling 7-Zip from the command line is: the name of 7-Zip executable (either 7z.exe or 7za.exe), followed by one of seven command a letters (e.g. “a” for adding to an archive, “u” for updating an archive, “x” for extracting from an archive, etc.), followed by the name of the archive file to be accessed (or created), followed by whatever additional information is necessary depending on which command is used (in the case of an “a” or “u” command, for example, it needs to know the names of the files and/or folders that are to be archived).
There are also 20 option switches available that can be used to customize how the commands operate. For example, “-r” tells 7-Zip to re-curse through all subdirectories, thus including all children, grandchildren, etc. of the specified folder(s).
Here are the seven commands that are available in 7-Zip:
| Command | Description |
|---|---|
| a | Add - create a new archive, or add files to an existing archive |
| d | Delete - remove files from an existing archive |
| e | Extract - unarchive files |
| l | List - display the contents of an archive |
| t | Test - validate the integrity of an archive |
| u | Update - overwrite existing files in an existing archive |
| x | Extract - same as “e”, except that the files are restored to their exact original locations (if possible) |
The difference between Add an Update is subtle. If the archive file you are creating does not already exist, then there is no difference between Add an Update (for all practical purposes). It is when the archive file does already exist, that the difference matters. In that case, Update will take the time to look for existing files in the archive that match the names (and paths) of the incoming files. For any match found, the previously existing file in the archive is first removed, and then the new file is added. In the case of the Add command, each new file is added to the end of the archive, regardless of whether or not a file by that name and path already exists. (Yes, that could mean that two files by the same name could exist and the archive files simultaneously, but it’s no big deal. It when extracting those files the older one will be extracted first, and then overwritten by the new one when it gets extracted.)
Making a Quick Backup: So, let’s say that there are two folders on your C: drive called C:\research and C:\papers that contain critical files that you are constantly editing. So, at the end of every day, you would like to be able to double-click an icon on your desktop which will archive all of the files in those folders. To do this, create a batch file that contains the following commands:
C: CD backups 7z.exe a research.7z C:research* -r 7z.exe a papers.7z C:papers* -r pause
My personal convention is to place such batch files in C:\sys\scripts (where “sys” stands for “system-level stuff”). Thus, this batch file would then be named something like “C:\sys\scripts\nightly_bu.bat”. To run the batch file, navigate to C:\sys\scripts (in the Windows Explorer), and then double-click on the name of the BAT file. (Or, create a shortcut to it on your desktop for convenience, and double-click on that.)
Excluding Files: Let’s say that the first time you run your batch file, it takes longer than you would like. Plus, you see that the research.7z file is much larger than you were expecting. Taking a closer look at the contents of C:\research\, you realize that there are quite a number of *.PDF documents that don’t really need to be backed up, since they never change and can easily be obtained again in case they are lost. Also, you use a text editor that creates backup copies of the files that are edited, by tacking on a “.bak” extension, and those don’t need to be archived either. To exclude such *.PDF and *.BAK files, change the batch file to look like this:
C: CD backups 7z.exe a research.7z C:research* -r -x!*.pdf -x!*.bak 7z.exe a papers.7z C:papers* -r -x!*.pdf -x!*.bak pause
-x is the file exclusion option switch. It is followed by either an exclamation point (!), or an at-sign (@). An exclamation point means that what follows is a wildcard pattern to be compared against the filenames found. An at-sign means that what follows is the name of a file that contains multiple wildcard patterns to be considered. So, in this case we actually had a choice. Instead of repeating the -x switch twice, once for the PDF files and once for the BAK files, we could have used something like “-x@exclude.txt”, where exclude.txt is a file that looks like this:
"*.pdf" *.bak
(Note: 7-Zip is unique in requiring the exclamation point before a literal wildcard. Usually, Windows programs that accept the @ notation for an option’s value will assume that the absence of an at-sign means that the option’s value is a literal.)
Other Option Switches: Some of 7-Zip’s more interesting option switches are:
| Switch | Description |
|---|---|
| -x | Exclude file(s), as shown above |
| -t | The type of archive to create (-t7z, -tzip, -tgzip, -tbzip2 or -ttar). -t7z is the default. |
| -r | Recurse subdirectories |
| -sfx | Create a self-extracting archive |
| -mx=9 | This can be any number from 0 to 9, where 0 means no compression (just store the files), and 9 means maximum compression (takes longer). -mx=5 is the default, a compromise between the amount of compression obtained, and the time required to perform the compression. |
| -o | specifies the output directory (for when extracting). The default is to use the current directory. |
| -u | Update options. this switch works in conjunction with the add, delete, and update commands to determine conflict resolution. For example, what happens when a file being added to an archive already exists in the archive and the timestamp on the source file is older than the timestamp in the archive. Should the file in the archive be left alone, or overwritten? |
| -v | Create Volumes — This switch allows you to specify the maximum size for an archive file. If the archive file would be bigger than that, 7-Zip will automatically split it into multiple volumes. this will ensure that its the archive files can fit on whatever storage media you have at hand. |
Read more: Software, Productivity

speedmaster wrote:
Its funny, for the longest time, this was the _only_ way to do it.
Posted 12 Jun 2007 at 4:37 pm ¶
n2989 wrote:
Nice tutorial i need an example of extracting to the identical path of the backup
ie. If i backup c:\myfiles\folder to an archive myfiles.7z when i restore (extract ) it i always get c:\myfiles\folder\folder when i want it back in the original path exactly.
I have used various combinations of 7za e and 7za x and still cant get it to work correctly ..
Thanks
Posted 14 Sep 2007 at 5:43 am ¶
kara wrote:
could you let me know unzip command line for 7z.exe?
1. zip the file
7z a -v 1440000 c:\a.zip c:a.\mdb
2. got the a.zip file
3. unzip the file
which command line can i use?
i want to unzip on this path “d:\a.mdb” file.
Reply me.
Posted 17 Sep 2007 at 7:48 pm ¶
Saudor wrote:
try using
7z -x -tzip a.zip
Posted 03 Oct 2007 at 11:03 am ¶
Gaiko wrote:
Thanks for the tutorial! I was having hard time making sense of the 7z help page (takes me back to the days of figuring out tar and compress via the man pages).
cheers
-Gaiko
Posted 15 Oct 2007 at 10:52 pm ¶
Marc wrote:
Can you run a bat file that runs in the background and the DOS window does not show up?
I want to be able to make a batch file to zip files but have my database program continue working.
Thanks
Posted 05 Dec 2007 at 11:47 am ¶
JT wrote:
> Saudor wrote:
> try using
> 7z -x -tzip a.zip
This syntax is incorrect,
The correct syntax is: 7z x -tzip a.zip
Posted 22 Jan 2008 at 11:46 am ¶
Dustin Bird wrote:
Can you please advise how to archive a folder but exclude a whole sub-directory? I have the following.
We have the following:
**PATH**\7z.exe” a -t7z -r D:\***PATH***\ftp.7z D:\FTPRoot
But i wish to exclude a whole subdirectory of the FTPRoot. Can you exclude whole folders?
Posted 14 Mar 2008 at 7:37 am ¶
Chris Johnson wrote:
How would you unzip multiple zip files or unzip just a .zip file in a directory, when you don’t know the name of the file? Your expedient assistance is appreciated. Thanks.
Chris
Posted 16 May 2008 at 8:08 am ¶
adrian shephard wrote:
How to archive some files using the encryption?
Posted 14 Jul 2008 at 11:48 am ¶
adrian shephard wrote:
how to archive in the silent mode if there is?
Posted 14 Jul 2008 at 12:02 pm ¶
Vlad wrote:
The 7zip, probably, is a good program but documentation lacks examples for basic cases.
I have an archive and output dir. I spent an hour and still can’t extract the archive into dir, using command line Nothing works. Will buy ZIP license; it’s chipper than such freeware.
Posted 16 Jul 2008 at 8:33 am ¶
Kyle wrote:
Took me longer than I liked to find this. Extract to a dir and always overwrite files, use -aoa.
*Path*\7z.exe x *Path*\foo.zip -aoa -o*Path*\outputDir
Posted 24 Jul 2008 at 11:23 am ¶
Joe Beck wrote:
How do I backup only files that do not have a extension and only have the archive bit set.
Posted 28 Aug 2008 at 12:01 pm ¶
ayzon wrote:
can any one tell me the all the codes that Skullptura using the game to compress with 7zip.? I need to back up my games.
Posted 09 Sep 2008 at 10:49 pm ¶
ayzon wrote:
##Skullpture##
** can any one tell me the all the codes that Skullptura using the game to compress with 7zip.? I need to back up my games.
Posted 09 Sep 2008 at 10:54 pm ¶
Kjetil Myhre wrote:
Is there any way of having 7zip delete the original file after a successful compression? (like the m command in WinRAR, which “moves” the file into an archive)
Posted 18 Sep 2008 at 2:17 am ¶
Dmitriy Chernyak wrote:
Is there any way to add encryption to the file using command line and how to unzip encrypted using command line ?
Posted 23 Sep 2008 at 6:51 am ¶
Anonymous Anon wrote:
@ayzon
Skullptura uses much more than 7zip. 7zip for his compressions is mostly just a container, plus he makes strong use of FreeARC/UHArc, not to mention an assortment of other compression tools designed specifically for audio and such.
Posted 16 Dec 2008 at 4:41 am ¶
Joseph Growney wrote:
I use your utility all the time and love it. I do have a challenge though. I have a web application that copies files from the client to the server, which takes about 5.5 minutes for a 16MB file. I am able to zip and copy the file on the client side in less than 1 minute but unzipping on the server side takes 4.5 minutes. Do you know of any way to speed up the unzip piece? I am using: 7za.exe x -tzip “”" & unzipFileName & “”" -aoa -o”"\\usrtapp005\f$\VIRTUAL_FILESERVER\Zip_Files\”"”
Thanks
Posted 02 Mar 2009 at 8:05 am ¶
Ankush wrote:
Can anyone help me for displaying/ capturing that the files are successfully zipped using 7-zip using command line prompt
Posted 09 Mar 2009 at 9:49 pm ¶
jt wrote:
@Kjetil Myhre
If I understand your question…you’re basically asking how to delete a file from a command line?
I’m no genius…but I assume there’s got to be a way
Posted 02 Apr 2009 at 8:22 am ¶
Adam wrote:
Not very helpful, jt.
Kjetil Myhre asked if 7zip has a move option, so that the file (or directory of files) will be deleted only upon successful compression into an archive.
Deleting the file or directory manually is not an acceptable workaround if the compression fails.
So, does 7zip have a move option or not?
Posted 07 Apr 2009 at 2:15 pm ¶
Craig wrote:
Just a reminder: Official support for 7-Zip is available via their forum on SourceForge. You can get to it from the 7-Zip support page: http://www.7-zip.org/support.html
Posted 07 Apr 2009 at 7:39 pm ¶
Dennis wrote:
@Dustin Bird
> We have the following:
> **PATH**\7z.exe” a -t7z -r D:\***PATH***\ftp.7z D:\FTPRoot
> But i wish to exclude a whole subdirectory of the FTPRoot.
> Can you exclude whole folders?
simply use:
“**PATH**\7z.exe” a -t7z -r “-x!foldertoexclude1″ “-x!foldertoexclude2″ “D:\***PATH***\ftp.7z” “D:\FTPRoot”
you can use the folder name and don’t need to use the full relative path, but then -like in the example- you have to use the “-r” option.
Posted 12 May 2009 at 8:23 am ¶
Horlando Nery wrote:
why WinExec(pchar(’command.com /c 7za.exe x Backup.7z -playout -y’),sw_shownormal); in delphi 7 is extracting always at same directorory where there are the application?
Posted 26 Jun 2009 at 12:07 pm ¶
Matthew Lederman wrote:
I have a self-extracting zip file that is in an .exe format…so when you click on it a “mini” unzip program pops up. This allows you to choose the directory to unzip to.
Is there any way to call something like this from a command line?
Thanks!
Posted 09 Jul 2009 at 11:29 am ¶
flacunsia wrote:
@Dennis
THANKS! dude, really
i have been for more than a hour trying to do this (exclude whole folder in the compression process) and then try with 7z and you give me the answer that i needed
thanks
Posted 16 Jul 2009 at 10:00 pm ¶
Paul K wrote:
I did not see an answer to the question: if 7zip can add the file into a zip file and upon a successful add delete - basically a move.
I have been using info-zip.org to do this, but it does not have the option to turn off the tree in the zip file.
Posted 27 Jul 2009 at 1:50 pm ¶
Arunabh Baruah wrote:
Please refer http://www.memecode.com/docs/winzip.html
This works.
Posted 12 Aug 2009 at 1:24 pm ¶
Bruce Baumann wrote:
How can I exclude system and hidden files from the archive? I’ve read the documentation and it doesn’t seem that there’s a way to exclude files based on the file’s attributes. This would be a great help. Also, is there a way to encrypt the archive as it is being created? I believe there is an option when creating a zip archive, but is there a way to encrypt a 7z archive? Thanks!
Posted 12 Aug 2009 at 7:28 pm ¶
UtCollector wrote:
I would like the command to zip files 1 by one.
at the moment i have the command: 7za a -mx9 files.7z *.* -mmt
problem is that it zippes al files in the directory into 1 file.
Posted 17 Sep 2009 at 7:15 pm ¶
MikeNYC wrote:
Is there Move functionality in the 7z commands that delete the existing file when it is is archived?
Posted 21 Sep 2009 at 12:23 pm ¶
nikki wrote:
‘move’ file into archive is easy. Just delete it afterwards. So, this:-
7z && del
Or ‘rmdir’ or such.
Posted 26 Sep 2009 at 8:01 am ¶
CJ wrote:
Hi all,
I have a question. I need to use 7zip command lines and need to know the following:
I have a path like: E\radio\2000\radio538 with submaps in this last one..
now i want to zip this. the name of the zipfile needs to be the last name in the path, so : radio538.. in that zipfile the map radio538 WITH all subfolders should be in this..
what command lines should i use?? what would be the exact command ???
help me please
Posted 09 Oct 2009 at 7:07 am ¶
Craig wrote:
I’ll remind you all again that you’ll have better luck getting answers to your various questions about 7-zip via the official support forum on SourceForge. You can get to the forum from the 7-Zip support page: http://www.7-zip.org/support.html. Of course, if anyone has specific questions about this introductory article of mine, I’ll be happy to answer them here. Otherwise, please do not expect to find general support here.
Posted 09 Oct 2009 at 12:28 pm ¶
Wiliam wrote:
If you someone could help me…
I am trying to write a batch file to compress all the folders in a folder
ex:
Maindir->folder1,folder2,folder3
how do I get:
Maindir-> folder1.7z,folder2.7z,folder3.7z
I only seem to be able to get the files…
Posted 21 Oct 2009 at 5:18 am ¶
Brij wrote:
Has anyone post the comand line utility for zipping the file I am trying below example but it gives an error
C:\ZipFileSoftware\7-Zip>7z.exe C:\ZipFileSoftware\test\*.txt C:\ZipFileSoftwar
e\test
Thanks
Brij
Posted 09 Nov 2009 at 4:18 pm ¶
Deniz wrote:
7za move workaround in a batch file:
7za a archive.7z files | find “Everything is Ok”
if ERRORLEVEL 1 goto :EOF
del files
:EOF
Posted 29 Dec 2009 at 2:44 pm ¶
Jean wrote:
Hello
I try to upade the files that are already in an archive.
I want to update ONLY those files, if needed.
My command line is:
7z.exe u h:\test\test.zip
but this command line adds all the files that are in the current folder to the archive test.zip
any idea ?
Posted 13 Jan 2010 at 8:47 am ¶
Garry wrote:
Need help !!
The syntax I use is 7za.exe x $base.zip -aoa
And the file is ex090103.zip,it extracts this file and I get ex090103.log
So finally I have both the files.
What I need is command line syntax to extract a .zip file and remove the archive file after the file is extracted
Thanks In advance,
Garry
Posted 19 Jan 2010 at 6:04 am ¶