Osx Command Line Zip

This article will help you understand how you can handle the common file formats TAR, GZIP, BZIP and ZIP on Linux operating systems (including CentOS, Ubuntu) and even some Unix-based OSes like MacOS (OS X) via the command line either via SSH or a local terminal session.

  1. Mac Command Line Zip With Password
  2. Mac Command Line Zip Multiple Files

How to Install the Needed Tools

MacOS – How to install 7zip so I can run it from Terminal on OS X. 7-zip macos terminal.app. I would like to be able to run 7zip from the command line on Mac. Does anyone have instructions on how to set this up? Zip archives can also be created from the command line by using the terminal command ‘zip’ with the following syntax: zip archive.zip file.txt Another simple way to create an archive from the command line is to use the Terminal’s drag & drop support, type out ‘zip’ as usual but then drop in the file(s) to compress into the Terminal. However, I am unable to find the wget command on OS X. How do download files from the web via the Mac OS X bash command line option? You need to use a tool (command) called curl. It is a tool to transfer data from or to a server, using one of the following supported protocols. RAR 5.0 archive format. RAR 5.0 archive format description. UnRAR for Windows. Command line freeware Windows UnRAR. WinRAR for Linux. WinRAR WINE wrapper allowing to run WinRAR in Linux. Follow instructions in ReadMe.txt file. This package is prepared by Karsten Pedersen. UnRAR for Windows CE phones. The corresponding gunzip and bunzip2 commands can be used to uncompress said archive, or you can just use flags on the tar command to perform the uncompression. If you are referring specifically to the Zip file format, you can simply use the zip and unzip commands. To compress: zip squash.zip file1 file2 file3 or to zip a directory.

On many Linux-like operating systems the command line tools for working with TAR, GZIP, BZIP and ZIP files are already installed, so you almost certainly don't need to install anything, but if you are running a minimal installation of your OS or if you've removed the tools in the past, follow the directions below to install what you need. Select the tab for the OS you are running:


The directions below assume you are running as the root user. If you are running as an another user, you may need to prepend the commands with sudo.

Example: sudo yum install ...

  1. Pull up a terminal session or log into your server/computer via SSH.
  2. The base repositories for these OSes have the packages we need. Execute the following command:

  3. If any of those are already installed, you will be informed. If any are missing, you will be asked if you want to install them. Answer y if everything looks ok.
  4. The system will download the needed packages and install them.

Now you should be able to follow the rest of the directions in this article.

The directions below assume you are running as the root user. If you are running as an another user, you may need to prepend the commands with sudo.

Example: sudo apt-get install ...

  1. Pull up a terminal session or log into your server/computer via SSH.
  2. The base repositories for these operating systems have the packages we need. Execute the following command:

  3. If any of those are already installed, you will be informed. If any are missing, you will be asked if you want to install them. Answer y if everything looks ok.
  4. The system will download the needed packages and install them.

Now you should be able to follow the rest of the directions in this article.

The needed command line tools ship with every version of MacOS/OS X since at least 10.6 Snow Leopard (and may also be installed in earlier versions we were unable to test).

To work with files via the command line, open the Terminal application located in /Applications/Utilities/Terminal(.app).

Once the terminal is open, you will be able to follow the rest of the directions in this article.

Remember, you can drag and drop files or folders into the terminal application and the full path to those items will be pasted automatically into the command line.


Working with TAR Files

The TAR file format is a very early archiving format that doesn't include any active compression by default. Often on Linux, items are tarred and then gzipped to compress them. TAR files typically end in .tar.

Mac Command Line Zip With Password

Put a Directory into a TAR File

Execute the following to create a single .tar file containing all of the contents of the specified directory:

Replace FILENAME with whatever filename you want and DIRECTORY with the path to the directory you want to make into a tarball.

Command Flags Explanation

c: Create a TAR file.
v: Output verbosely (you'll be told exactly what is happening in detail).
f: Specify a filename for the resulting TAR file.

Put a Directory into a TAR file and Compress it with GZIP

Execute the following to create a single .tar.gz file containing all of the contents of the specified directory:

Replace FILENAME with whatever filename you want and DIRECTORY with the path to the directory you want to make into a compressed tarball.

Tarred files compressed with GZIP sometimes use the .tgz file extension.

Command Flags Explanation

c: Create a TAR file.
v: Output verbosely (you'll be told exactly what is happening in detail).
f: Specify a filename for the resulting TAR file.
z: Compress the TAR file with GZIP

Put a Directory into a TAR file and Compress it with BZIP2

Execute the following to create a single .tar.bz2 file containing all of the contents of the specified directory compressed with BZIP. (BZIP typically produces smaller files than GZIP, at the cost of more processing time.):

Replace FILENAME with whatever filename you want and DIRECTORY with the path to the directory you want to make into a compressed tarball.

Command Flags Explanation

c: Create a TAR file.
v: Output verbosely (you'll be told exactly what is happening in detail).
f: Specify a filename for the resulting TAR file.
j: Compress the TAR file with BZIP2

Extract Items from TAR Files

Execute the following command to extract files and directories from an uncompressed .tar file:

Replace FILE with the filename of the file you are trying to uncompress. The file will uncompress into the current directory.

Command Flags Explanation

x: Extract the contents from the file specified.
v: Output verbosely (you'll be told exactly what is happening in detail).
f: Specify a filename to uncompress.

Extract Items from GZIPPED Tarball File

Execute the following command to extract files and directories from a GZIP compressed TAR file:

Replace FILE with the filename of the file you are trying to uncompress. The file will uncompress into the current directory.

Command Flags Explanation

x: Extract the contents from the file specified.
v: Output verbosely (you'll be told exactly what is happening in detail).
f: Specify a filename to uncompress.
z: Uncompress the tarball via GZIP.

Extract Items from BZIPPED Tarball File

Execute the following command to extract files and directories from a BZIP compressed TAR file:

Replace FILE with the filename of the file you are trying to uncompress. The file will uncompress into the current directory.

Command Flags Explanation

x: Extract the contents from the file specified.
v: Output verbosely (you'll be told exactly what is happening in detail).
f: Specify a filename to uncompress.
j: Uncompress the tarball via BZIP2.


If you'd rather specify a different directory to extract files to rather than just dumping everything in the current directory add -C /PATH/TO/DIRECTORY/ to the commands above. Replace /PATH/TO/DIRECTORY/ with the actual path to the directory where you want the files to be placed.


Working with Zip Files

Zip is probably the most common compressed archiving format in the world. Zip files usually end in .zip.

Compress a Directory Full of Files into a ZIP File

Execute the following command to place everything inside a specified directory into a compressed ZIP file.

Command Flags Explanation

-r: Recursively compress all files and directories contained within DIRECTORY/ in the zip file (otherwise you only get the top level files).

Uncompress a ZIP file Into the Current Directory

Execute the following command to uncompress the items in the ZIP file into the current directory.

TheDeveloperBlog.com

Home Contact Us

CSharp Java Python Swift GO WPF Ruby Scala F# JavaScript

<< Back to C-SHARP
Use 7-Zip on the command line. Compress, extract, archive and optimize with the 7za.exe executable.
7-Zip. The 7za.exe program is used to compress, extract and update files through the command line. It provides superior compression. It is a great program.
7-Zip is open-source. This makes it easy to obtain and use. This document covers some common command lines (focused on Windows, but applicable to any OS like Linux or Mac OS X).
Start. Download the 7-Zip command line executable: 7za.exe. This is the exe you will use to run commands on archives. Please go to 7-zip.org and get the command line version.

Tip: For convenience and so you don't need to change environment paths, put the 7za.exe file in your user directory.

Next: Open the Windows console and test the 7za.exe program out with a few commands. Type in the exe name 7za and this will display.

Grammar: We see the grammar we need to use with 7za.exe. The 'command' is the main verb.

Switches: Then you specify optional switches, the archive name (source or destination) and files. My user directory is 'C:UsersSam'.

7-Zip default output7-Zip (A) 4.60 beta Copyright (c) 1999-2008 Igor Pavlov 2008-08-19Usage: 7za <command> [<switches>...] <archive_name> [<file_names>...] [<@listfiles...>]
Command a. You can use the 'a' command with the single letter a. This command stands for 'archive' or 'add.' Use it to put files in an archive.

Arguments: You have to specify the destination archive, and the source files (in that order).

Note: On the test system, 'C:UsersSam' contains two files (file1.txt and file2.txt). The command puts those two files in an archive.

Tip: To open your archive, right click on it and select 7-Zip -> Open archive. The screenshot shows the files compressed in files.7z.

Example command line 1C:UsersSam>7za a -t7z files.7z *.txt7-Zip (A) 4.60 beta Copyright (c) 1999-2008 Igor Pavlov 2008-08-19ScanningCreating archive files.7zCompressing file1.txtCompressing file2.txtEverything is OkC:UsersSam>
Command d. We use the 'd' command in 7-Zip command lines. This stands for delete. It allows you to remove a certain file (or set of files) from inside an archive.

Note: You will need this if you use huge archives and need to save time. This is from the manual.

Tip: You can also remove a single file from an archive with 'd'. This is more useful when you do not have a solid archive.

Example d command line7z d archive.zip *.bak -r7z: use executabled: delete filesarchive.zip: delete from this archive*.bak: only match bak files-r: traverse all subdirectories
Command e. The 'e' stands for extract, and it means to unzip or expand an archive. You must specify the source archive always, and may also specify a destination.

Info: The 'e' command extracts everything to a specified directory. Another command 'x' can preserve directory structures in archives.

Overwrite prompts: 7-Zip will always prompt you if there is a file it needs to overwrite to extract the new file.

However: This can be problematic if you are scripting or embedding 7za.exe. In that case, see the -y switch.

Example e command line7z e archive.zip7z: executablee: use extract commandarchive.zip: source archive you want to expand
Command l. The lowercase L is used to list the contents of archives. You probably will not need to use it often. I thought I would test it and show an example.

Next: This shows the listing of a solid archive. The originals are 27216 bytes and 3888 bytes. They compress down to 1030 bytes.

Example l command lineC:UsersSam>7za l files.7z7-Zip (A) 4.60 beta Copyright (c) 1999-2008 Igor Pavlov 2008-08-19Listing archive: files.7zMethod = LZMASolid = +Blocks = 1Physical Size = 1202Headers Size = 172 Date Time Attr Size Compressed Name------------------- ----- ------------ ------------ ------------------------2008-10-02 15:48:01 ....A 27216 1030 file1.txt2008-10-02 15:47:45 ....A 3888 file2.txt------------------- ----- ------------ ------------ ------------------------ 31104 1030 2 files, 0 folders
Command t. Here we use the 't' command in the 7z program. This command allows you to test the integrity of archives. It stands for 'test' and is much less useful than the '-t' switch.

Warning: Don't confuse the two. This one is used for diagnostics. We usually want the hyphen '-t.'

Example t command line7z t archive.zip *.doc -r7z: use this executablet: test the specified archivearchive.zip: the archive you want to test*.doc: test all these files in the archive-r: recurse all child directories
Command u. This stands for update. This command replaces old files in your archive with newer files. This prevents needing to decompress and recompress the entire archive.

Warning: The 'u' command doesn't work with solid archives. A solid archive is one where all the files are compressed together.

So: You cannot update specific files in solid archives with the 'u' command. Solid archives are limited.

Example u command line7z u archive.zip *.doc7z: executable nameu: update commandarchive.zip: archive you want to update files in*.doc: only update these files (Word documents)
Switch m. We can change the optimization settings in 7-Zip on the command line. This is the most important and useful option you can use.

Tip: It specifies the method of compression. Here I will show a bunch of options, and also some examples.

Compression levels:Switch -mx0: Don't compress at all. This is called 'copy mode.'Switch -mx1: Low compression. This is called 'fastest' mode.Switch -mx3: Fast compression mode. Will automatically set various parameters.Switch -mx5: Same as above, but 'normal.'Switch -mx7: This means 'maximum' compression.Switch -mx9: This means 'ultra' compression. You probably want to use this.
Switch m, advanced. Here are advanced compression method (-m) switches. The first three are usually of limited use. My experience is that manual optimizations doesn't produce big benefits.

Switch -mfb: Specifies number of fast bytes. Sometimes helps with 'sparse' files. Don't bother.

Switch -mpass: Number of passes for deflate compression. Don't bother with this. Automatically set with levels.

Switch -md: This specifies dictionary size. It is automatically set, so don't bother.

Switch -mmt: Enable multithreading. Use if you have quad-core and a huge archive. Specify 'on' or 'off'.

Command x. This command is like 'e' except it preserves the full paths. If you have an elaborate or important directory structure, use this option. This would be most useful for backups.
Example x command line7z x archive.zip7z: executable namex: use the extract commandarchive.zip: the archive you want to extract all the files from
Switch t type. Here I show how to specify the archive type. Note that you can specify any file name you want for any type. But some extensions are recommended—they are standard.
Type switches Switch: -t7z Format: 7ZExample filename: archive.7z (default option) Switch: -tgzip Format: GZIPExample filename: archive.gzip, archive.gz Switch: -tzip Format: ZIPExample filename: archive.zip (compatible) Switch: -tbzip2 Format: BZIP2Example filename: archive.bzip2 Switch: -ttar Format: TARExample filename: tarball.tar (UNIX and Linux) Switch: -tiso Format: ISOExample filename: image.iso (may not be supported) Switch: -tudf Format: UDFExample filename: disk.udf
Type switch examples. The 7-Zip manual shows the -tiso and -tudf switches. These are not the most common. Almost all of the examples in this document use -t switches.
Example a command lines7z a -tiso archive.iso7z a -tudf archive.udf7z: executable namea: add to archive-tiso or -tudf: format of archive to createarchive.iso or archive.udf: name of archive to create
Solid archives. 7z is the only file format in 7-Zip that you can specify whether the archive is solid or not. Solid makes it impossible to use the 'u' command to update individual files.

Switch -ms=on: Enable solid mode. This is the default so you won't often need to specify it.

Switch -ms=off: Disable solid mode. Useful when you need to update individual files. Will reduce compression ratios normally.

7z archives. Some things you can change are dictionary sizes, FastBytes values, MatchFinder values, and filters. Normally you don't need to deal with these.
PPMd. With the 7z format, you can specify the algorithm. PPMd is good for compressing plain text files. It is ideal for Word documents. PPMd does not perform as well on binary data.

PPMd switch -mmem=24b, -mmem=24k, -mmem=24m: These control the amount of memory you use. They are useful and higher is normally better.

PPMd switch -mo=2, -mo=32: These specify the model order in PPMd. They are not normally useful.

Tip: You should use PPMd when you have a large corpus (body) of text. This could include HTML. It can improve ratios by around 30%.

Example commands. Here I show the example compression commands from the 7-Zip manual. I demonstrated simple ones at the start of this document. These are more complex.
Example a command line 27z a -tzip archive.zip *.jpg -mx07z: name of executablea: add to archive command-tzip: specify a ZIP archive (useful for compatibility)archive.zip: destination archive*.jpg: only add jpg files to archive-mx0: don't compress, just copy useful for already-compressed files
Example of 7z format. This next command line shows how to create a solid 7z archive of program files (executables). It uses multithreading mode, which means it will be fast.
Example ms command line7z a -t7z archive.7z *.exe *.dll -ms -mmt7z: name of executablea: archive command specified-t7z: use 7z file type (less compatible and smaller results)archive.7z: destination archive file*.exe: include all *.exe files in directory in new archive*.dll: include all *.dll files in new archive-ms: create solid archive (default)-mmt: multithread the operation (faster)
Create PPMd archive. PPMd is an extraordinary algorithm for compressing text. Here I show a command in the 7-Zip manual that compresses all the text files in the working directory.

Tip: The command is useful because you will normally want to compress only text files with PPMd.

Example PPMd command line7z a -t7z archive.7z *.txt -m0=PPMd7z: executable name/patha: add command specified-t7z: use the 7z format (needed for PPMd)archive.7z: destination archive file*.txt: select all text files-mo=PPMd: compress with this algorithm
Switch o. Sometimes you do not want to extract to the current directory. This is where -o can come in handy. Use this to set the destination directory.
Example o command line7z x archive.zip -oC:Doc7z: executable namex: extract archive with paths intactarchive.zip: archive to extract files from-oC:Doc: extract all files to the Doc folder on the C: drive
Switch p. We can use the '-p' switch, which refers to the word 'password'. This is really helpful when security and encryption is involved. You can specify a password on the command line.
Example p command line7za a pw.7z *.txt -pSECRET7za: name and path of 7-Zip executablea: add to archivepw.7z: name of destination archive*.txt: add all text files to destination archive-pSECRET: specify the password 'SECRET'
Opening password-protected archives. This next console output shows what happens when you try to open the password-protected archive. The password here is SECRET. I am so clever.

Header encryption: Add -mhe to encrypt headers. The password command will automatically deal with encrypted headers.

Tip: Remember, encrypted headers will hide the names of the files in your archive.

Example x command line 2C:UsersSam>7za x pw.7z7-Zip (A) 4.60 beta Copyright (c) 1999-2008 Igor Pavlov 2008-08-19Processing archive: pw.7zEnter password:
More switches. Here we take a closer look at more switches that are of limited use. They are sometimes useful to know. Usually you can do better just by using the defaults.

Switch -ssc: Specify case-sensitive mode. The default is -ssc- on Windows (insensitive). The default is -scc on Linux (sensitive).

Switch -ssw: Compress locked files. You can try this if you have problems opening files.

Switch -w: Set working directory. You can use this when you want to specify temp folders.

Case-sensitive. We can use case-insensitive file names. For cross-platform stuff, the case-sensitive option is useful. I will show my own example here with some explanation.
Example ssc command line7za.exe a archive.7z Z*.* -ssc7za.exe: 7-Zip command-line executable path and namea: archive commandarchive.7z: add files to this target archiveZ*.*: select only files whose first letter is a capital Z-ssc: case-insensitive matching
WithSwitch v. In data compression, a volume is a segment of a data set that is a certain number of bytes long. The volume switch specifies the exact size in bytes, kilobytes or megabytes.

Also: You can specify sequential volumes with the 'v' switch on the 7za.exe command line.

Switch ao. The 'ao' switch allows you to specify whether you want to overwrite old files. Be careful—you cannot restore an overwritten file normally. This switch takes another argument.

Switch -aoa: This switch overwrites all destination files. Use it when the new versions are preferred.

Switch -aos: Skip over existing files without overwriting. Use this for files where the earliest version is most important.

Switch -aou: Avoid name collisions. New files extracted will have a number appending to their names. You will have to deal with them later.

Switch -aot: Rename existing files. This will not rename the new files, just the old ones already there.

Examples:7z x test.zip -aoa7z: use the 7-zip executablex: use the extract commandtest.zip: extract files from this archive-aoa: overwrite all existing files
Multiple files. To add many files to one archive, please use the 'a' command and the wildcard * symbol. Specify the name of the destination archive file and the source files afterwards.

Mac Command Line Zip Multiple Files

How do I add many files with a specific extension? Use the 'a' command and the wildcard * symbol, but specify the extension after the wildcard.

Example: For example, *.txt means all text files. You can use the wildcard anywhere.

How can I add many files from an entire subdirectory? Specify just the directory name. You do not need to use a wildcard. This command specifies an entire directory called 'subdir'.
Example subdirectory command line7z a -tzip archive.zip subdir7z: use executablea: add to archive-tzip: use zip compressionarchive.zip: create this archivesubdir: source directory
How do I use BZip2? You can use BZip2 by specifying the '-tbzip2' switch. This can be combined with any compression level. The modes in 7-Zip automatically use many different settings.
How do I use 7z format? Specify the '-t7z' switch for type. Or you can simply omit the type switch and that will default to 7z. This format offers the greatest compression ratios.
Prompts. You can stop 7-Zip from displaying prompts. Please use the -y switch. This will assume a yes answer to all prompts. Use this only when you are confident.
Why can't I update my archive? It is probably a solid archive. 7z archives are by default solid archives—all the files are compressed together.

Tip: Change the archive not to be solid if you want to update it. Search this page for 'solid'.

Can I specify the output directory? Use the 'e' command and combine it with the -o switch. The syntax with -o is a bit funny. This is the example from the 7-Zip help file.
Example output directory command line7z e archive.zip -oC:soft *.cpp -r7z: executablee: use extract commandarchive.zip: source archive you want to extract from-oC:soft: the destination folder (-o is the switch and C:soft is the argument)*.cpp: only extract cpp files (C++)-r: traverse all subdirectories
How can I see what's inside an archive? Use the 'l' command. You might want to use 'l' in a utility that you run from a command line to make sure your batch archiving properly works.
How can I exclude certain files? Sometimes you want to manually exclude certain files. Use the -x switch, followed immediately with an exclamation mark and then the filename.

So: If you want to exclude 'file1.txt', use the switch '-x!file1.txt'. Please include the hyphen and exclamation.

How can I replace files? By using the -ao switch, described above. There are other options, and it is usually a better idea to use one of the renaming options (-aou or -aot).
Can I ignore extracting files already on disk? Yes—specify the -aos option, which means 'skip overwriting files.' This will cause 7za.exe to not copy the newer files out of the archive.

Note: Use -aos if your files don't change over time and overwriting would just be a waste.

Embed. You can embed 7-Zip in a Windows .NET program. This yields the same great compression but in your own GUI. The link shows some compression ratios.7-Zip Executable
Internal settings. You can change internal settings. You do not need to do this normally. I recommend just using the mx=0 (and 3, 5, 7, 9) settings. An in-depth study would be fascinating.

Filters: You can change compression filters, which change behaviors on executable files such as *.exe and *.dll.

Files

And: You can enable header compression and encryption (-mhc=on and -mhe=on). Header compression is by default enabled.

Zopfli. If you are reading this page, you probably want even better compression. I have found that Zopfli can create gzip archives a few bytes smaller than 7-Zip can. It is slow to run.Zopfli: github.com
AdvanceCOMP. You can use AdvanceCOMP to improve compression ratios. The improvement is often small, less than 1%. AdvanceCOMP has more options and is more fine-grained.AdvanceMAME: github.com
A review. 7-Zip can be used on the command line. We created new archives, added to existing archives, used different formats, and used various strengths.

.Net

.NETArrayDictionaryListString2DAsyncDataTableDatesDateTimeEnumFileForForeachFormatIEnumerableIfIndexOfLambdaLINQParsePathProcessPropertyRegexReplaceSortSplitStaticStringBuilderSubstringSwitchTuple

Java

CoreArrayArrayListHashMapString2DCastCharacterConsoleDequeDuplicatesFileForFormatHashSetIfIndexOfLambdaMathParseIntProcessRandomRegexReplaceSortSplitStringBuilderSubstringSwitchVectorWhile