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.

CD \books
7z.exe u -tzip 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:

CD \backups
7z.exe a research.7z C:\research\* -r
7z.exe a papers.7z C:\papers\* -r

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:

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

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


(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.


  1. Its funny, for the longest time, this was the _only_ way to do it. ;-)

  2. 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 ..


  3. could you let me know unzip command line for 7z.exe?

    1. zip the file
    7z a -v 1440000 c:\ c:a.\mdb
    2. got the file
    3. unzip the file
    which command line can i use?
    i want to unzip on this path “d:\a.mdb” file.

    Reply me.

  4. try using

    7z -x -tzip

  5. 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).



  6. 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.


  7. > Saudor wrote:
    > try using
    > 7z -x -tzip

    This syntax is incorrect,
    The correct syntax is: 7z x -tzip

  8. 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?

  9. 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.


  10. How to archive some files using the encryption?

  11. how to archive in the silent mode if there is?

  12. 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.

  13. Took me longer than I liked to find this. Extract to a dir and always overwrite files, use -aoa.

    *Path*\7z.exe x *Path*\ -aoa -o*Path*\outputDir

  14. How do I backup only files that do not have a extension and only have the archive bit set.

  15. 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.

  16. ##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.

  17. 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)

  18. Is there any way to add encryption to the file using command line and how to unzip encrypted using command line ?

  19. @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.

  20. 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\”””


  21. Can anyone help me for displaying/ capturing that the files are successfully zipped using 7-zip using command line prompt

  22. @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 ;)

  23. 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?

  24. 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:

  25. @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.

  26. why WinExec(pchar(‘ /c 7za.exe x Backup.7z -playout -y’),sw_shownormal); in delphi 7 is extracting always at same directorory where there are the application?

  27. 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?

  28. @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 :D


  29. 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 to do this, but it does not have the option to turn off the tree in the zip file.

  30. Please refer

    This works.

  31. 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!

  32. 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.

  33. Is there Move functionality in the 7z commands that delete the existing file when it is is archived?

  34. ‘move’ file into archive is easy. Just delete it afterwards. So, this:-

    7z && del

    Or ‘rmdir’ or such.

  35. 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 :(

  36. 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: 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.

  37. If you someone could help me…

    I am trying to write a batch file to compress all the folders in a folder

    how do I get:
    Maindir-> folder1.7z,folder2.7z,folder3.7z

    I only seem to be able to get the files…

  38. 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


  39. 7za move workaround in a batch file:

    7za a archive.7z files | find “Everything is Ok”

    if ERRORLEVEL 1 goto :EOF

    del files


  40. 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\

    but this command line adds all the files that are in the current folder to the archive

    any idea ?

  41. Need help !!
    The syntax I use is 7za.exe x $ -aoa
    And the file is,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,

  42. Is anyone aware of a switch that will allow me to archive files that match a specific date, or that are created in the current month. I’m trying to archive log files and want the archive created to be named the current month and year and then I want only log files for the current month placed into that archive. Any suggestions?

  43. > Is anyone aware of a switch that will allow me to
    > archive files that match a specific date, or that
    > are created in the current month.

    I found this last night and modified it, “forfiles.exe /p c:\path-to-files /m *.type-of-file /d -30 /c “cmd.exe /c c:progra~1\7-zip\7z.exe u @path && del @path”

    This should add files older then three days (/d -3) to the archive and then delete them. No checking for failure here though.

  44. guys can anyone tell me the right format for encrypting files w/ password using 7z.exe?

  45. Hi there,
    I’ve downloaded 7zip 4.65 (command line) to extract a tar file.
    I simply want to extract a file individually which I can do, but also rename it on extraction (to effectively remove a timestamp the third party suplier adds to the end of the file).
    I haven’t been able to find any information anywhere. I tried Sourceforge and the threads for each forum seemed to be on individual links!!

  46. Put this in a DOS .bat file and every single file will be compressed and deleted after completion.

    for %%f in (*.emf) do “C:\Program Files\7-zip\7z.exe” a -tgzip -mx9 “%%~nf”.emz “%%f” && del “%%f”

    Tested and works like a charm.

  47. I’m trying to zip a file with a the current year and month; right now it zips as 2010MM. I’m using the -a command and my file path and name. I tried putting the -tf behind -a but it’s not working. What am I missing?

  48. how do I make “7z.exe l ” display the file contents alphabetically?

  49. how do I make “7z.exe l” display the file contents alphabetically?

  50. A huge question.

    1.ok I have SQL server with three instances and it backsup to a local drive for now. Drive d:
    2.Each database has its own backup directory.(d:\instancename\Northwind\ ; d:\instancename\solar)
    3.I want to keep the file name of the backupfile.bak as is just with a 7z at the end.
    4. after zipping the file I would like to move the file to a remote server \\server\sqlbackup\* directory name of database* in point 2.
    5. remove the files after X amount of days and replaced with new ones.

  51. Encrypting with password:
    7z.exe a -mx=9 -mhe -pPASSWORD “d:\backupfile.bak.7z” “c:\backupfile.bak”

    Low priority:
    start “7z.exe” /b /belownormal /wait “7z.exe” a -mx=9 -mhe -pPASSWORD “d:\backupfile.7z” “c:\backupfile.bak”

    chuck: just make a .bat/.cmd script with multiple commands. Have it backup then
    move /y “d:\backupfile.7z” “\server\sqlbackup\whatever”
    You can make Task Scheduler run it as often as you want.

  52. mechanics: Thanks for the tip it was helpful. that solves my point 4. I have made the batch script already ad working great.
    The files are currently getting backed up I just want to 7zip them in -mx=9 with the current file name.
    Then after 20days I need the oldest file to be overwritten by a newer file. Thank you in advance

  53. “move to archive” for 7zip batch. As i did not finde script like this, i made it for myself. maybe helpfull for others. If the ziping process faild then the script stops and waiting for interaction( the folders wont be deleted)

    @echo off
    REM zip folders
    for /d %%X in (*) do “c:\Program Files\7-Zip\7z.exe” a “” “%%X\” -tzip -mx9
    REM del folders
    for /d %%d in (*.*) do rmdir /s /q “%%d”

  54. how do we wait for the command line process to finish ?
    I used system() for 7zip command line mode.
    I use the c++ builder XE.

  55. How can I zip with 7zip to multiple archives size 700Mb.

    I have a lot of folders and subfolders which I like to zip into multiple zip files large 700Mb…

  56. ‘7z’ is not recognized as an internal or external command, operable program or batch file.

    Did I do something wrong? CD to the path of the exe works but not calling on 7z.

  57. Hi Guys,

    Is there a was of burning these zipped files to a CD from the command encrypted??

    Thks!! :)

  58. Hey, I’m really new at this, and I’m trying to make an auto installer for some minecraft mods. But I can’t get past the basic step of extracting files from .zip.

    So I’ve used
    cd c:\program files (x86)\7-zip
    7z.exe e c:\users\mike\downloads\mc_mods\ -o c:\users\mike\desktop\new folder
    as a test, but it won’t extract. What am I doing wrong?
    Thanks for any replies

  59. First, thanks for this post !
    How do I tell 7z.exe to only pull the files into the new zip file and not the folder name?.

    For example,
    In the following directory structure if I have d:/testa/testb
    and I run 7z from testa, make it zip all the contents of testb into
    a zip file, this contains even the directory ‘testb” when I dont want it to, I want only the contents of the folder

    Have searched for answer but found none…any ideas ?

  60. I would like to zip up multiple folders that are more than 1 day old and the zipped folder must retain the original folder name.

    The folders will contain files but I do not want to zip these files inside the folders.

  61. i need to get file Ziped ..
    C:/Backup should be transfer to D:/ Newbackup
    How to do that one

  62. What is the option to enable LZMA2 compression using 7-zip 9.20 ?

  63. For encrypting via script use:
    zip –encrypt –password 123456 foo

    Works with Unix zip 3.0 and above.

  64. Installed 7-zip, I want to zip Folder2 and Folder4 (specific folders) with subfolders and files using 7-zip. How can I specify multiple folders on the commandline? How you do that?


  65. Test 7zissue1 abc contain lot of different file
    How it is possible to archive Main directory with sub-directory in one zip

    string sourceName = @”d:\Test 7zissue1 abc\*.*”;
    string targetName = @”d:\Test 7zissue1 abcd xyz\”;
    // 1
    // Initialize process information.
    ProcessStartInfo p = new ProcessStartInfo();
    p.FileName = @”C:\Users\dev4\Desktop\7zipIssue\7ZIPISSUE\7ZIPISSUE\bin\Debug\7z.exe”;

    // 2
    // Use 7-zip
    // specify a=archive and -tgzip=gzip
    // and then target file in quotes followed by source file in quotes
    p.Arguments = “a -tgzip -r \”” + targetName + “\” \”” + sourceName + “\” -mx=9″;
    p.WindowStyle = ProcessWindowStyle.Hidden;

    // 3.
    // Start process and wait for it to exit
    Process x = Process.Start(p);

  66. I see your blog needs some fresh content. Writing manually is time consuming, but there is solution for this hard task.
    Just search for; Miftolo’s tools rewriter

  67. Hello. I see that you don’t update your site too often. I know
    that writing articles is boring and time consuming. But did you know that there is a tool that allows you to create new posts using existing content (from
    article directories or other pages from your niche)? And it
    does it very well. The new articles are unique and
    pass the copyscape test. You should try miftolo’s tools

  68. I have noticed you don’t monetize your blog,
    don’t waste your traffic, you can earn additional cash every month.
    You can use the best adsense alternative for any type of website (they approve all websites),
    for more details simply search in gooogle: boorfe’s tips monetize your website

Post a Comment

Your email is never published nor shared. Required fields are marked *

© 2006-2007 Maxim Software Corp.  All rights reserved.