
The Zip command in Linux is a powerful utility that compresses files and directories into a single archive file using the .zip format. Compressing files reduces the size and helps to pack multiple files together for faster data transfer or storage. Many operating systems, such as Windows, macOS, Linux, and Unix support the Zip format, allowing you to pack and extract files across major platforms.
This article explains how to use the zip command in Linux to effectively compress and optimize files.
zip Command Syntax in LinuxThe following is a basic zip command syntax:
In the above zip command:
[options]: Includes optional flags that modify the command's behavior.zipfile: Specifies the name of the resulting Zip archive.files: Defines the files or directories you want to compress.Follow the steps below to create sample files and directories to test the zip command in linux:
Switch to your user's home directory.
Create multiple sample text files. For instance, file.txt, file1.txt, file2.txt, newfile.txt, and file.log.
Create sample directories, such as dir and dir1. Then create a sub_dir sub-directory under dir1.
Switch to the dir directory.
Create new files in the directory, such as file1.txt, file2.txt, sys.log, and auth.log.
zip Command OptionsThe following are the most common zip command options:
zip Command in Linux with ExamplesFollow the steps below to run some zip command examples:
Compress a single file.txt file into a Zip archive and name it archive.zip.
Output:
Compress file.txt, file1.txt, and file2.txt files into a single Zip archive. For instance, archive.zip.
Output:
Compress the dir directory and all its contents including sub-directories into archive.zip.
Output:
Exclude specific files from the archive, such as all .log files.
Output:
Add the newfile.txt file to an existing archive.zip archive.
Output:
List the contents of the archive.zip archive using the -sf option.
Output:
Overwrite or update the archive.zip archive with file1.txt and file2.txt. The command replaces file1.txt and file2.txt in the archive if the files exist.
Output:
Delete specific files, such as file1.txt from the archive.zip
Output:
Extract files from the archive.zip archive.
Output:
Specify the -9 option to compress files with the best compression method and reduce the archive size. The command may take more time to complete.
Output:
Compress the file1.txt and file2.txt files and encrypt the archive.zip archive with a password. When prompted, enter a password. You'll need it when unzipping the file.
Output:
zip Command OptionsUse the -j option to compress the file1.txt and file2.txt files without including the directory paths in the archive.
Output:
Select and compress multiple *.txt files using the wildcard character *.
Output:
Use the -v option to display detailed output when compressing the file1.txt and file2.txt files.
Output:
Compress the file1.txt and file2.txt files into archive.zip archive, encrypt the archive, and display detailed output.
Output:
Compress the file1.txt and file2.txt files into archive.zip while deleting the source files.
Output:
Unzip the archive.zip archive into the dir1 directory.
Output:
Create a new archive.zip archive and compress its contents using a different compression level, such as -9.
Output:
Handle file conflicts in the archive.zip archive using the u option. For instance, the following command updates the archive with newer versions of files to avoid overwriting files in the existing archive. -n .txt instructs the command only to update the .txt files.
Output:
zip with Other CommandsYou can combine the zip command with other commands for advanced use cases as detailed below:
Combine find and zip commands to find and compress files modified in the last 24 hours under the /home/vultr_user/dir directory. The zip archive.zip -@ inputs the file paths from the find command results and adds them to the archive.zip file.
Output:
Combine find and zip commands to find and compress files larger than a specific size like 10MB.
Output:
Pipe the output of the ls -l /home/vultr_user/dir command to the zip command. The command lists all files in the /home/vultr_user/dir directory and compresses them into archive.zip. The hyphen - option allows the zip command to read from standard input.
Output:
You have used the Zip command in Linux to compress files and directories. The zip command reduces storage size, improves data transfer, and allows file portability. For more information and configuration options, run man zip to view the Zip command's manual page.
0 Comments
Be the first to comment and share your perspective with the community.