ZIP Command
The zip command is used to compress files and directories into a .zip archive. This reduces the file size and makes it easier to transfer or store multiple files together in a single archive.
Initial Example
Using zip to compress a file called example.txt into an archive called archive.zip:
zip archive.zip example.txt
Examples
1. Compress Multiple Files
To compress multiple files into a single archive, specify their names followed by the archive name:
zip archive.zip file1.txt file2.txt file3.txt
2. Compress a Directory
To compress an entire directory and its contents, use the -r (recursive) option:
zip -r archive.zip directory_name
3. Add Files to an Existing Archive
To add files to an existing .zip archive, use the same command with the additional files:
zip archive.zip newfile1.txt newfile2.txt
4. Exclude Specific Files
To exclude specific files from the archive, use the -x option followed by the pattern to exclude:
zip -r archive.zip directory_name -x "*.log"
5. Password-Protect an Archive
To password-protect a .zip archive, use the -e (encrypt) option:
zip -e archive.zip file1.txt file2.txt
UNZIP Command
The unzip command is used to extract files and directories from a .zip archive. This allows you to access the compressed files and use them as needed.
Initial Example
Using unzip to extract the contents of an archive called archive.zip:
unzip archive.zip
Examples
1. Extract to a Specific Directory
To extract the contents of a .zip archive to a specific directory, use the -d option followed by the directory path:
unzip archive.zip -d /path/to/destination
2. List Contents of an Archive
To list the contents of a .zip archive without extracting them, use the -l option:
unzip -l archive.zip
3. Extract Specific Files
To extract specific files from an archive, specify the file names after the archive name:
unzip archive.zip file1.txt file2.txt
4. Overwrite Existing Files Without Prompting
To overwrite existing files without prompting for confirmation, use the -o option:
unzip -o archive.zip
5. Password-Protected Archives
To extract files from a password-protected archive, you’ll be prompted to enter the password:
unzip archive.zip


