The Command to Unzip Files in Unix
The Unix, Linux and Mac OS X command to unzip archives from the terminal is named, appropriately enough, "Unzip." Unzip has a wealth of options that you can read about by typing "man unzip" into your terminal, but there are only a few options that are used commonly.
-
The Basic Command
-
At its most basic, the unzip command takes the following form: "unzip file.zip." This command will take the file named "file.zip" and extract its contents into the currently open directory. It is fast and simple, and most of the time is exactly what you need.
Extracting to Another Directory
-
If you need to extract the files in the zip archive into another directory, then you must use the "-d" (directory) flag. It is done by typing "unzip file.zip -d directory." This will take all the files from the "file.zip" file and put them into the directory.
-
Extracting Only New Files
-
Sometimes, especially when zip files are used for keeping team members' documents in sync, you will want to extract only the information from the zip file that is different than the files you already have. For this, you must use the "-f" (freshen) flag. Type the following: "unzip -f file.zip." This will compare the files in "file.zip" with the files in the current directory. Any file in "file.zip," but not in the directory, will be extracted. Furthermore, any file in "file.zip" that was altered more recently than an existing file in the directory will be extracted. The rest are left alone.
Extract to Standard Out
-
Most of the time, you need to extract the files from a zip directly to the hard-drive. However, there are instances when it may be more useful to extract the files directly to the operating systems "standard output." For example, if you administer a Web server that needs to extract documents from a zip file before transferring them over the Internet, extracting to the hard disk before making the transfer is slow, wasteful and pointless. Instead, an experienced developer can reroute the system's "standard output" to the Internet connection and use the "-p" (pipe) flag to "pipe" the data from the zip file onto the Internet, using "unzip -p file.zip." Be warned, however: By default, the standard output is set to your current terminal, so running this command directly from the terminal without rerouting the output will result in megabytes of gibberish being spewed to your command prompt.
Convert Text While Extracting
-
Unfortunately, when the operating systems now in common use, from Unix to Windows, were devised, they each solved the problem of how to represent ordinary text in slightly different ways. For example, operating systems do not agree on how to treat line-endings in ordinary text files. In order to prevent trouble, "unzip" can serve as a translator for all text documents within the zip file. By using the "-a" flag, unzip will translate all text files to use the conventions of the current operating system as it extracts. The following command will be "unzip -a file.zip."
-