Updated July 19, 2023
Introduction to Zip Command in Linux
ZIP is a file packaging utility compression technique in Unix. Each file is stored in a single file with the extension.zip.This zip command in Linux is usually supported by almost all operating systems like MSDOS, OS/2, Window NT, Minix, and Macintosh, etc. Compress and tar is used with this Zip command and compatible with PKZIP(Phil Katz’s ZIP for MSDOS systems).
Syntax:
zip [with some options] zipFIle fileDir/fileList
The zip file is a new or existing zip archive and fileDir/fileList is the path occasionally including wildcards. when the same name of zip archive is found then it will update it.
Example: If folder/file_1 and folder/file_2 is contained in folder.zip and the directory folder contains the files folder/file_1 and folder/file_3. So, if before a zip command will execute, folder.zip has:
folder/file_1
folder/file_2
Also, when the directory folder has:
file_1
file_3
and folder.zip will have now:
folder/file_1
folder/file_2
folder/file_3
Where folder/file_1 is replaced whereas folder/file_3 is new.So, folder.zip now contains folder/file_1, folder/file_2, and folder/file_3, along folder/file_2 from before that are unchanged.
Syntax to Extract Zip Files in Linux
Unzip command extracts list files from the ZIP archive on Unix systems. When no option is used to extract it into the present working directory (and sub-directories which are inside it) every file from the specified ZIP archive.
Syntax:
$unzip fold.zip
Zip Command Options in Linux
Now let us focus on the Zip command Options below:
1) -u Option
Updates the file in the zip archive. This also updates an existing entry in the zip archive, and only if it has been modified more now than the version already in the zip archive.
Command:
$zip –u file_name.zip file.txt
Suppose we have the files in the current directory are listed below:
file1.txt
file2.txt
file3.txt
file4.txt
Command:
$zip –u file_name.zip file5.txt
After updating file5.txt from file_name.zip file, the files will be restored with unzip command.
Command:
$unzip file_name.zip
$ls command
Output:
file1.txt
file2.txt
file3.txt
file4.txt
file5.txt
The file5.txt file is updated to the zip file.
2) -d Option
It deletes the file from the zip archive. This option deletes the created zip file. Suppose we have the files in the current directory are listed below:
file1.txt,file2.txt,file3.txt,file4.txt,file5.txt
Syntax:
$zip –d file_name.zip file.txt
Command:
$zip –d file_name.zip file5.txt
After removing file5.txt from file_name.zip file, the files will be restored using unzip command
Command:
$unzip file_name.zip
$ls command
Output:
file1.txt
file2.txt
file3.txt
file4.txt
The file5.txt file is removed from zip file
3) -m Option
Will delete original/main files after zipping. It will move the files by making zip and delete the original files/folder.
If a directory becomes empty after deleting files, the respective directory is also deleted. No deletions are completed until the zip has created archive without any error. So this is useful in maintaining disk space, but ultimately unsafe while removing all input files.
Syntax:
$zip –m file_name.zip file.txt
Suppose we have the files in the current directory are listed below:
file_1.txt,file_2.txt,file_3.txt,file_4.txt
Command :
$zip –m file_name.zip *.txt
After the execution of this command on the terminal here is the result:
Command:
$ls command
Output:
file_name.zip
//No other files as .txt(extension) are found
4) -x Option
It will exclude files when we are going to create the zip. Let say you are going to zip all the files in the present directory and want to exclude few files which are not needed. So you can exclude these files which are not needed using the -x option.
Syntax:
$zip –x file_name.zip file_to_be_excluded
Suppose we have the files in the current directory are listed below:
file_1.txt,file_2.txt,file_3.txt,file_4.txt
Command:
$zip –x file_name.zip file_3.txt
This command while executing will compress every file except file_3.txt
Command:
$ls command
Output:
file_name.zip // compressed file
file_3.txt //this file will be excluded while doing compression
5) -r Option
It will recursively zip the files inside it and then folders inside it.
Syntax:
$zip –r file_name.zip directory_name
Suppose we have the files in the current directory (doc) are listed below:
a.pdf
b.pdf
c.pdf
Command:
$zip –r filedir.zip doc
Output:
adding: doc/ //Compressing the directory
adding: doc/a.pdf // Compressing first file
adding: doc/b.pdf // Compressing second file
adding: doc/c.pdf //Compressing the third file
6) -v Option
Using the Verbose mode option we will print diagnostic version information. This option will display the progress indicator during compression and request verbose information about the zip structure.
Syntax:
$zip –v file_name.zip file.txt
Suppose we have the files in the current directory are listed below:
file_1.txt,file_2.txt,file_3.txt,file_4.txt
Command:
$zip -v file1.zip *.txt
Output:
adding: file_1.txt (in=0) (out=0) (stored 0%)
adding: file_2.txt (in=0) (out=0) (stored 0%)
adding: file_3.txt (in=0) (out=0) (stored 0%)
adding: file_4.txt (in=0) (out=0) (stored 0%)
total bytes=0, compressed=0 -> 0% savings
Uses of Zip Command in Linux
The uses of Zip Command in Linux are as follows.
- ZIP is a perfect technique when there is less bandwidth or internet speed and you want to transfer a bunch of files. Then now using this command you can zip and transfer the files in a very efficient manner.
- The zip program puts one or more compressed files into a single zip archive, along with information like name, path, date, time of last modification, protection, and check information to verify file integrity. An entire directory structure can be compressed into a zip archive by a single command.
- 2:1 to 3:1 as the compression ratio is prevalent for text files either uses a deflation method or store files without compression. This will choose automatically the best for each file to be compressed.
- It comes very handy for archiving files; packaging a set of files for distribution and for saving disk space for a short time by compressing unused files or directories.
Conclusion
Now we have understood the depression (unzip) and compression (zip) concept and we have seen how to manipulate compressed files using different options. ZIP is a perfect technique when there is less bandwidth or internet speed and you want to transfer a bunch of files. Then now using this command you can zip and transfer the files in a very efficient manner.
Recommended Articles
This is a guide to Zip Command in Linux. Here we discuss the syntax to extract zip files along with the different commands of zip in Linux. You may also look at the following articles to learn more –