0
0
Linux CLIscripting~5 mins

zip and unzip in Linux CLI - Commands & Configuration

Choose your learning style9 modes available
Introduction
Sometimes you need to bundle many files into one to save space or send them easily. Zip compresses files into one package, and unzip extracts them back to normal files.
When you want to send multiple documents in one file via email.
When you need to save disk space by compressing old project files.
When you want to download a group of files from the internet as one package.
When you want to backup a folder quickly into a single compressed file.
When you want to extract files from a downloaded zip archive.
Commands
This command creates a zip file named archive.zip containing file1.txt and file2.txt. It bundles and compresses these files into one package.
Terminal
zip archive.zip file1.txt file2.txt
Expected OutputExpected
adding: file1.txt (deflated 50%) adding: file2.txt (deflated 45%)
This command extracts all files from archive.zip into the current directory, restoring the original files.
Terminal
unzip archive.zip
Expected OutputExpected
Archive: archive.zip inflating: file1.txt inflating: file2.txt
This command creates a zip file named archive_folder.zip containing the entire folder 'myfolder' and its contents recursively.
Terminal
zip -r archive_folder.zip myfolder
Expected OutputExpected
adding: myfolder/ adding: myfolder/fileA.txt (deflated 40%) adding: myfolder/subfolder/ adding: myfolder/subfolder/fileB.txt (deflated 30%)
-r - Recursively include all files and folders inside the specified directory
This command lists the contents of archive.zip without extracting them, so you can see what files are inside.
Terminal
unzip -l archive.zip
Expected OutputExpected
Archive: archive.zip Length Date Time Name --------- ---------- ----- ---- 1234 2024-06-01 12:00 file1.txt 2345 2024-06-01 12:00 file2.txt --------- ------- 3579 2 files
-l - List contents of the zip file without extracting
Key Concept

If you remember nothing else from this pattern, remember: zip bundles and compresses files into one package, and unzip extracts them back to normal files.

Common Mistakes
Trying to unzip a file that does not exist or has a different name.
The unzip command will fail with an error because it cannot find the specified zip file.
Check the exact zip file name and path before running unzip.
Using zip without the -r flag to compress a folder.
Only the folder itself is zipped, not its contents, so the zip file will be mostly empty.
Use zip -r to include all files and subfolders inside the folder.
Extracting zip files into a directory without write permission.
Unzip will fail to write files, causing errors and incomplete extraction.
Make sure you have write permission in the target directory or change to a writable directory.
Summary
Use zip to compress one or more files into a single archive file.
Use unzip to extract files from a zip archive back to normal files.
Use the -r flag with zip to compress entire folders recursively.
Use unzip -l to list contents of a zip file without extracting.