Home »
Linux
ZIP files on command line in Linux
By IncludeHelp Last updated : October 20, 2024
ZIP files
The most popular archiving utility in the Microsoft world is the ZIP file. It is not as established in Linux but is well sustained by the zip and unzip commands.
Like we have discussed earlier, we can use TAR or Gzip/bzip2 for compression or decompression, another popular alternative is ZIP.
Example 1: Zipping
Let's try with archiving some files and compressing them.
Let's break the code...
Syntax of ZIP
We have syntax of ZIP is:
zip ZIP_name Filename1 filename2 ...
So the first argument is ZIP_name that is the name of ZIP we wanted to be followed by list of files, in our case, we used wild-character (*) for the list of files. Zip provides a verbose output the files and the compression ratio, unlike in Tar or gzip.
Zip will not recurse into any subdirectories by default so we need to use –r option explicitly.
Example 2
In the above example (1), we can't recurse into subdirectories by default like we saw in case if tar but with option r we can:
In the above example, in our folder (or directory) name 'File' contain 10 .jpg files and 1 zip file and now this will let zip command to recurse through subdirectories.
In the aboveexample (2), all files under 'File' directory added because of the –r option we used.
We can list files in the zip or unzip command by using –l option (list):
Example 3: Unzipping
For extracting files from the zip, we use unzip command, it is same as creating an archive.
Syntax
unzip ZipName.zip
As in Example 1, we will have the same option except this time files will extract. Let's unzip the file with –l option:
In the above image, we can see that we used –l option for listing, which produces size date of creation and name of the file on output.
Note: In case, we want to extract specific files we can use the following format:
unzip ZIPname.zip filename
With the above command, only those specific file will extract which are mention there.
Recommended Articles