Joinsubscribers and get a daily digest of news, geek trivia, and our feature articles. You can create a zip archive or unzip files from one with some common Linux terminal commands. Thanks to the dominance of the ZIP format in the Windows realm, ZIP files are probably the most common form of compressed archive in the world.
And, if you want to archive some files and send them to a Windows user, the ZIP format will be the easiest, most compatible solution for everyone. You may already know that Linux and Unix-like operating systems such as macOS have tools to allow you to create ZIP files and extract files from them, called zip and unzip. We checked some Linux distributions to see whether they included these utilities in the standard installation. All of the utilities were present in Ubuntu They were also present in Manjaro Fedora 29 included zip and unzipbut none of the other utilities and that was also the case for CentOS.
To create a ZIP file, you need to tell zip the name of the archive file and which files to include in it. The name of the file and the amount of compression that was achieved on that file is shown. If you do not want to see the output from zip as the ZIP file is created, use the -q quiet option. To include sub-directories in the ZIP file, use the -r recursive option and include the name of the sub-directory on the command line.
To create a ZIP file as before and also include the archive sub-directory, use this command. When the person who receives the ZIP file extracts it, all of the files are placed neatly within a directory on their computer.
Note that this command is being issued from the parent directory of the work folder. You can set how much compression is applied to the files as they are added to the ZIP archive. The range is from 0 to 9, with 0 being no compression at all. The higher the compression, the longer it takes to create the ZIP file.
But then, for modestly sized ZIP files, the default compression level 6 is probably good enough anyway. The default compression level is 6. There is no need to provide the -6 option, but it will do no harm if you do. With the selection of files and directories being archived here, the difference between no compression level 0 and the default compression level 6 is K.
The difference between the default compression and the highest level of compression level 9 is only 4K. Adding passwords to ZIP files is easy. All of the files that are extracted have the owner set to the user who is extracting them. To have the files extracted in a specific directory, use the -d directory option, and provide the path to the directory you wish the archive to be extracted into.
If a ZIP file has been created with a password, unzip will ask you for the password. If you do not want to extract a particular file or group of files, use the -x exclude option. Suppose you have extracted an archive but you have deleted a few of the extracted files by mistake. A quick fix for that would be to extract the files once again. But if you try to extract the ZIP file in the same directory as before, unzip will prompt you for a decision regarding overwriting the files.
It will expect one of the following responses. To force unzip to overwrite any existing files use the -o overwrite option. The most efficient way to replace the missing files would be to have unzip only extract any files in the archive that are not in the target directory. To do this, use the -n never overwrite option.
It is often useful and instructive to see a list of the files inside a ZIP file before you extract it.Tip: For convenience and so you don't need to change environment paths, put the 7za.
Next: Open the Windows console and test the 7za. Type in the exe name 7za and this will display. Grammar: We see the grammar we need to use with 7za. The "command" is the main verb. Switches: Then you specify optional switches, the archive name source or destination and files. You can use the "a" command with the single letter a. This command stands for "archive" or "add. Arguments: You have to specify the destination archive, and the source files in that order.
The command puts those two files in an archive. The screenshot shows the files compressed in files. Compress, extract, archive and optimize with the 7za. The 7za. It provides superior compression. It is a great program. This makes it easy to obtain and use. Download the 7-Zip command line executable: 7za.
The dark mode beta is finally here. Change your preferences any time. Stack Overflow for Teams is a private, secure spot for you and your coworkers to find and share information.
Here's one solution that extracts all zip files to the working directory and involves the find command and a while loop:. A solution that correctly handles all file names including newlines and extracts into a directory that is at the same location as the file, just with the extension removed:.
Note that you can easily make it handle more file types such as. Travel the directory structure recursively. If any of the file names specified on the command line are directories, gzip will descend into the directory and compress all the files it finds there or decompress them in the case of gunzip. I realise this is very old, but it was among the first hits on Google when I was looking for a solution to something similar, so I'll post what I did here.
My scenario is slightly different as I basically just wanted to fully explode a jar, along with all jars contained within it, so I wrote the following bash functions:. If you're storing the functions in a file loaded on non-interactive shells e.
Hopefully this will help someone. A little warning - explodeFile also deletes the ziped file, you can of course change that by commenting out the last line.
Learn more. How do you recursively unzip archives in a directory and its subdirectories from the Unix command-line? Ask Question. Asked 11 years, 6 months ago. Active 1 month ago. Viewed k times.
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 formatyou can simply use the zip and unzip commands. There are a truly vast number of different ways to compress and uncompress under UNIX derivatives so I'm going to assume you meant "zip" in the generic sense rather than a specific file format.
That's assuming of course that you have a tar capable of doing the compression as well as combining of files into one. If not, you can just use tar cvf followed by gzip again, if available for compression and gunzip followed by tar xvf. For specific handling of ZIP format files, I would recommend downloading 7zip and using that - it recognises a huge variety of file formats, including the ZIP one. If you don't have zip and unzip packages installed and you have java, you can use jar to unzip:.
Well, when it comes to distributing files for a variety of operating systems, I'd recommend 7-zip. Usually in the package p7zipyou'll get the 7z and 7za command, with which you can create your own 7z archives. I recommend this method in case Windows users can't open 7z archives in case you want to advice a tool for that: PeaZip.
If you want to use the same compression algorithm with your tarballs, use the -J switch with tar :. It even works as a filter. You'll need to make sure these commands are installed via your package manager. It's no harder than using anything else on the command line. It's certainly simpler than creating archives with tar. The most standard answer is paxwhich is recommended over cpio and tar. This behavior is different from zipwhich compresses each file before putting it in the archive.
For that, I've started to use dtrx. The whole point of it is to remove complexity. So you just pass in any archive to it and it figures out what needs to be done with it. Zip, tar, gz Use is always the same — dtrx archive. It asks a few questions and you're done. I often find myself trying to zip the modified files in my work directory -- either to copy all the modified files to another sandbox work directory or to preserve them for a short while because I want to undo some of the changes.
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It only takes a minute to sign up. If that doesn't help, can you provide some details about the commands you've tried and the output? Maybe it could be done with a script, but in command line if you want to delete a directory from the zip file, make sure the directory is empty.
If the directory is not empty, then first delete all the files from the directory. Then you can able to delete the directory. Ubuntu Community Ask! Sign up to join this community. The best answers are voted up and rise to the top. Home Questions Tags Users Unanswered. How do I delete a directory from a zip file using a bash script? Ask Question. Asked 8 years, 11 months ago. Active 4 months ago.
Viewed 39k times. UAdapter UAdapter Active Oldest Votes. If you want to delete all files from a specific dir, you could do this: zip --delete file. Eric Carvalho Can you give command lines? Sign up or log in Sign up using Google. Sign up using Facebook. Sign up using Email and Password.There are a number of different ways to compress files using the Linux command line.Moving and Copying Files and Directories in Linux
This article includes practical examples that show how to use the zip command to compact and organize files within your file system. Zipped files are used when you need to save space and copy large files from one place to another. If you have 10 files that are all megabytes in size and you need to transfer them to an ftp site, the transfer could take a considerable amount of time depending on your processor speed.
If you compress all 10 files into a single zipped archive and the compression reduces the file size to 50MB per file, then you only have to transfer half as much data. The previous command is fine for archiving all the files in a folder but it only includes files that aren't hidden. It's not always this simple. Imagine you wanted to zip your home folder so that you can back it up to a USB drive or external hard drive.
Your home folder includes hidden files. This creates a file called home. You must be in the home folder for this to work. The problem with this command is that it only includes the files in the home folder and not the folders, which brings us to the next example. If you want to add new files to an existing archive or update the files in an archive, use the same name for the archive file when running the zip command. For example, imagine you have a music folder with four albums in it and you create an archive called music.
Now imagine one week later you download two new albums. To add the new albums to the zip file, simply run the same zip command as you did the previous week. If the zip file has a list of files in it and one of the files on the disk has changed, then the amended file is updated in the zip file. If you have a zip file that is supposed to contain the same file names every time and you want to update that file with any changes that have been made to those files then the -f switch helps you do this.
When you run the following command the zip file will contain the updated files file4 and file5 but file7 and file8 will not be added. So you created a massive zip file with hundreds of files and now realize that there are four or five files in the zip file that you don't need there.
Without having to zip all those files again, you can just run the zip command with the -d switch as follows:. If you have files in one zip file and you want to copy them to another zip file without extracting them first and rezipping them, use the -u switch.
Assume you have a zip file called "variousmusic. If the zip file you are copying to doesn't exist, it is created. The next switch is a really useful one because it lets you use the output of other commands to insert files into your zip file. Assume you want to create a file called lovesongs. The above command isn't percent perfect because it picks up words like "clover" as well, but you get the idea.
To add all the returned results from the above command to a zip file called lovesongs. If you are backing up your computer but the only media you have available for backing up to is a set of blank DVDsthen you have a choice.
You can keep zipping files until the zip file is 4. If you have a zip archive that is broken, you can try and fix it using the -F command and if that fails, the FF command. This is useful if you have created a split archive using the -s switch, and you lost one of the archive files.
If you have sensitive information that you want to store in a zip file, use the -e command to encrypt it.
You are asked to enter a password and to repeat the password. If you know you are going to be creating a large archive, make sure that the correct files are going to be added to the zip file. You can see the expected results of a zip command by specifying the -sf switch.
After backing up files to a zip file, it is tempting to save disk space by deleting the original files.A separate companion program, unzipunpacks and uncompresses zip archives. The zip program puts one or more compressed files into a single zip archive, along with information about the files name, path, date, time of last modification, protection, and check information to verify file integrity. An entire directory structure can be packed into a zip archive with a single command.
Compression ratios of to are common for text files. If bzip2 support is added, zip can also compress using bzip2 compression, but such entries require a reasonably modern unzip to decompress. When bzip2 compression is selected, it replaces deflation as the default method. When given the name of an existing zip archive, zip will replace identically named entries in the zip archive matching the relative names as stored in the archive or add entries for new names.
For example, if foo. After this, foo. For example.
Under Unix, this option can be used to powerful effect in conjunction with the find command. For example, to archive all the C source files in the current directory and its subdirectories:. Streaming input and output. For example:. This generally produces better compression than the previous example using the -r option because zip can take advantage of redundancy between files.
The backup can be restored using the command:. When no zip file name is given and stdout is not a terminal, zip acts as a filter, compressing standard input to standard output. If Zip64 support for large files and archives is enabled and zip is used as a filter, zip creates a Zip64 archive that requires a PKZIP 4. To avoid amgibuities in the zip file structure as defined in the current zip standard PKWARE AppNote where the decision to use Zip64 needs to be made before data is written for the entry, but for a stream the size of the data is not known at that point.
If the data is known to be smaller than 4 GB, the option -fz- can be used to prevent use of Zip64, but zip will exit with an error if Zip64 was in fact needed. Also, zip removes the Zip64 extensions if not needed when archive entries are copied see the -U --copy option. When directing the output to another file, note that all options should be before the redirection including -x.
When changing an existing zip archive, zip will write a temporary file with the new contents, and only replace the old one when the process of creating the new version has been completed without error. If the name of the zip archive does not contain an extension, the extension. If the name already contains an extension other than.