- ZIP command in Linux with examples
- Options in ‘Zip’ command in Linux
- Examples of ‘Zip’ command in Linux
- 1) `unzip` command in ‘Zip’
- 2) `-d` Option in Zip command
- 3) `-u` option in Zip command
- 4) `-m` option in Zip command
- 5) `-r` option in Zip command
- 6) `-x` option in Zip command
- 7) `-v` options in Zip command
- Most Frequently asked Question about `zip` command in Linux
- 1) How do I create a zip file in Linux?
- 2) How do I extract a zip file in linux?
- 3) How do i exclude specific files from a zip archive?
- 4) How do I include hidden files in a zip archive?
- 5) How do I password-protect a zip archive?
- Conclusion
- Remove single file from zip archive on Linux
- 1 Answer 1
- Linux zip Command Tutorial with Examples
- zip Command Syntax
- Install zip Command
- zip Command Help
- Zip A File
- Zip Multiple Files
- Add New File To Zipped File
- Delete/Remove A File From Zip File
- Zip Files and Folders Recursively
- Print Details About Zip Operation, Files and Folders
- Suppress Output Do Not Print Information
ZIP command in Linux with examples
ZIP is a compression and file packaging utility for Unix. Each file is stored in a single .zip file with the extension .zip.
- Zip is used to compress files to reduce file size and is also used as a file package utility. Zip is available in many operating systems like Unix, Linux, windows, etc.
- If you have limited bandwidth between two servers and want to transfer the files faster, then zip the files and transfer them.
- 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 2:1 to 3:1 is common for text files. zip has one compression method (deflation) and can also store files without compression. zip automatically chooses the better of the two for each file to be compressed. The program is useful for packaging a set of files for distribution, archiving files, and for saving disk space by temporarily compressing unused files or directories.
zip [options] [file_name.zip] [files_names]
The syntax for Creating a zip file:
zip [file_name.zip] [file_name]
Options in ‘Zip’ command in Linux
(Remove files from the archive):
This option allows you to remove specific files from a zip archive. After creating a zip file, you can selectively remove files using the -d option.
zip -d [file_name.zip] [files_name]
(Update files in the archive):
The -u option enables you to update files in an existing zip archive. You can specify a list of files to update or add new files to the archive. The update occurs only if the modified version is more recent than the one already in the zip archive.
zip -u [file_name.zip] [files_name]
(Move files into the archive):
With the -m option, you can move specified files into the zip archive. This operation also deletes the target directories or files after creating the zip archive. If a directory becomes empty after removing the files, it is also deleted. Use this option with caution, as it permanently removes the input files.
zip -m [file_name.zip] [files_name]
(Recursively zip a directory):
The -r option allows you to recursively zip a directory and its files. It includes all the files present in the specified directory and its subdirectories in the resulting zip archive.
zip -r [file_name.zip] [directory_name]
(Exclude files from the zip):
Using the -x option, you can exclude specific files from being included in the zip archive. This is useful when you want to zip all files in a directory but want to exclude certain unwanted files.
zip -r [file_name.zip] -x [directory_name]
(Verbose mode):
The -v option enables the verbose mode, providing diagnostic information during compression. It displays a progress indicator and offers verbose diagnostic information about the zip file structure. When used alone, it prints a diagnostic screen along with details about the zip executable and the target environment.
zip -v [file_name.zip] [file_name]
Examples of ‘Zip’ command in Linux
1) `unzip` command in ‘Zip’
unzip will list, test, or extract files from a ZIP archive, commonly found on Unix systems. The default behavior (with no options) is to extract into the current directory (and sub-directories below it) all files from the specified ZIP archive.
Suppose we have a zip file “name = jayesh_gfg.zip” and we have three text files inside it “name = a.txt, b.txt and c.txt”. we have to unzip it in the current directory.
Syntax and Output:
Here, we used `ls` command to display all the files that has be unzipped from the zipped file.
2) `-d` Option in Zip command
zip -d [file_name.zip] [files_name]
Suppose we have zip file “name = myfile.zip” and have eight files in it “name = hello1.c, hello2.c, hello3.c, hello4.c, hello5.c, hello6.c, hello7.c, hello8.c “.
We have to delete hello7.c, then…
Syntax and Output:
- First, we have deleted `hello7.c` successfully.
- Then we used “sudo unzip myfile.zip” to unzip the file for confirming that our file is deleted.
- Then we used “ls” to see the file that had been unzipped.
delete a file from zip file
3) `-u` option in Zip command
zip -u [file_name.zip] [files_name]
Suppose we have zip file “name= myfile.zip” and we have to add a new file “name = hello9.c” in it.
Syntax and Output:
we have used `vi` to see that our file is added successfully.
4) `-m` option in Zip command
zip -m [file_name.zip] [files_name]
Suppose we have zip file “name= myfile.zip” and we have to move files “name = hello1.c, hello2.c, hello3.c, hello4.c, hello5.c, hello6.c, hello8.c, hello9.c ” Present in current directory to zip file.
Syntax and Output:
we have used `ls` to see that our files are moved successfully.
To check files inside “myfile.zip” we can type “vi myfile.zip”.
moved files inside zip file
5) `-r` option in Zip command
zip -r [file_name.zip] [directory_name]
Suppose we have zip file “name= myfile.zip” and we have to move files “name = hello1.c, hello2.c, hello3.c, hello4.c, hello5.c, hello6.c, hello7.c, hello8.c ” present in directory “name= jkj_gfg” to zip file recursively.
Syntax and Output:
To check files inside “myfile.zip” we can type “vi myfile.zip”.
copy file recursively form a directory to a zip file
6) `-x` option in Zip command
zip -r [file_name.zip] -x [directory_name]
Suppose we have zip file “name= myfile.zip” and we have to move files “name = hello1.c, hello2.c, hello3.c, hello4.c, hello5.c, hello6.c, hello7.c, hello8.c ” present in directory “name= jkj_gfg” to zip file recursively.
Syntax and Output:
Here, the -r option is used to recursively add all files and directories in the current directory to the archive, and the. specifies the current directory as the source directory. The -x a.txt option excludes the file “a.txt” from the archive.
To check files inside “myfile.zip” we can type “vi myfile.zip”.
file copied recursively except one file we mentioned.
7) `-v` options in Zip command
zip -v [file_name.zip] [file_name]
If we want to know about all the files with extension “.c”
Syntax and Output:
checking information about all files inside zip
Most Frequently asked Question about `zip` command in Linux
1) How do I create a zip file in Linux?
By using `zip` command itself.
Example:
We are creating a zip file named = `gfg.zip` containing the files `first.txt` and `second.txt`:
zip gfg.zip first.txt second.txt
2) How do I extract a zip file in linux?
By using `unzip` command.
Example:
we are extracting file name = `gfg.zip`:
3) How do i exclude specific files from a zip archive?
By using `-x` option in zip command.
Example:
To exclude specific files from a zip archive, we can use the -x option followed by the name of the file you want to exclude. For example, the following command creates a zip file named `gfg.zip` containing all files in the current directory except for `third.txt`:
4) How do I include hidden files in a zip archive?
By just using `-r` option, which is copying every file recursively. For example, our zip file name is ‘gfg.zip` and we want to archive hidden files of the current directory.
5) How do I password-protect a zip archive?
By using option `-P` followed by the password we want to use. For example, our zip file name = `gfg.zip` which contain file name = `first.txt` and `second.txt`. And we also used `-e` to encrypt our zip archive, so whenever a user extracts the archive file, a prompt will come to enter the password.
zip -e -P [my_password] gfg.zip first.txt second.txt
Conclusion
Zip command in Linux is used to compress files and packaging them into a single .zip archive, which overall helps us in saving disk space and making it easy to handle big data. We have discussed various options used in zip command like -d, -u, -m, -r, -x, and -v. Overall, it is a recommended tool for Linux users to efficiently manage their files.
Remove single file from zip archive on Linux
I have a zip archive (created automatically in a script with -j -r flags) and I would like to remove a single file from it. I tried as documented.
zip -d "picture_43_9.jpg" gallery.zip
zip warning: picture_43_9.jpg not found or empty zip error: Nothing to do! (picture_43_9.jpg)
unzip -l . /gallery.zip | grep -i 43_9.jpg 1477092 2013-05-22 14:23 picture_43_9.jpg
1 Answer 1
You have the arguments swapped. Try this:
zip -d gallery.zip "picture_43_9.jpg"
-d
—delete
Remove (delete) entries from a zip archive. For example:zip -d foo foo/tom/junk foo/harry/\* \*.o
will remove the entry foo/tom/junk , all of the files that start with foo/harry/ , and all of the files that end with .o (in any path). Note that shell pathname expansion has been inhibited with backslashes, so that zip can see the asterisks, enabling zip to match on the contents of the zip archive instead of the contents of the current directory. (The backslashes are not used on MSDOS-based platforms.) Can also use quotes to escape the asterisks as in
zip -d foo foo/tom/junk «foo/harry/*» «*.o»
Not escaping the asterisks on a system where the shell expands wildcards could result in the asterisks being converted to a list of files in the current directory and that list used to delete entries from the archive.
Under MSDOS, -d is case sensitive when it matches names in the zip archive. This requires that file names be entered in upper case if they were zipped by PKZIP on an MSDOS system. (We considered making this case insensitive on systems where paths were case insensitive, but it is possible the archive came from a system where case does matter and the archive could include both Bar and bar as separate files in the archive.) But see the new option -ic to ignore case in the archive.
Linux zip Command Tutorial with Examples
Zip is a compression algorithm and format used to package given files and folders to use less disk space. The zip files have generally the *.zip extension and different tools that can be used to create, add, remove or extract zip files. But the most prolific tool and command is the zip command in Linux operating systems.
zip Command Syntax
The zip command syntax is like below.
zip OPTION ZIP_FILE FILES_FOLDERS
- OPTION is used to provide different options to the zip file.
- ZIP_FILE is the file name of the zip file.
- FILES_FOLDERS can be single or more files and folders which will be zipped into the ZIP_FILE.
Install zip Command
In some cases the zip command is not installed by default. We may need to install zip command explicitly.
Install zip Command In Ubuntu, Debian, Mint, Kali:
Install zip Command In Fedora, RHEL, CentOS:
zip Command Help
The zip command is very old and actively developed command and provides detailed help information about its usage. The simplest and less informative way to get help is running the zip command without any option or parameter.
More detailed help can be listed with the -h2 option to the zip command like below.
Zip A File
Let’s start with a simple example where we zip a single file named cities.txt . The zip file is named as cities.zip like below. There is no need to add an extra parameter to zip it.
adding: cities.txt (deflated 15%)
Zip Multiple Files
We can also zip multipe files in a single step. We just provide the files we want to zip. In the following example we zip files named cities.txt , MyFile.csv and myshell.txt into zip file named my.zip .
Add New File To Zipped File
New files can be added to the existing zip file. Just provide the zip file name and the file name we want to add. In the following example we add the file numbers.txt into the zip file named my.zip .
Delete/Remove A File From Zip File
A single file can be removed from the zipped file. The -d option is used to remove the specified file. In the following example, we remove the file named myshell.txt from the zipped file named my.zip .
If the specified file is deleted or removed from the zip file the previous deleting: line is printed with the deleted file name.
Zip Files and Folders Recursively
Normally when we specify a folder to zip only the folder is zipped and its contents are not zipped. In order to zip a folder or directory with its files and child folders and directories it should be zipped recursively. The -r option is used to zip files and folders recursively.
$ zip -r Downloads.zip Downloads/
Print Details About Zip Operation, Files and Folders
The zip command and operation executes different steps. More detailed information about the zip operation can be printed as verbose mode. The -v option is used to print detailed verbosely which can be also called as debugging the zip.
$ zip -r -v Downloads.zip Downloads/
Suppress Output Do Not Print Information
We can prevent the zip command printing information about the zip process and related files and folders names. We should suppress the output with the -q option like below.
$ zip -r -q Downloads.zip Downloads/